there is such a code

String file = "C:\\Users\\Администратор\\Desktop\\ewq\\asd.xlsx"; // TODO Auto-generated method stub File excel = new File (file); FileInputStream fis = new FileInputStream(excel); XSSFWorkbook wb = new XSSFWorkbook(fis); XSSFSheet ws = wb.getSheetAt(0); int rowNum = ws.getLastRowNum() + 1; int colNum = ws.getRow(0).getLastCellNum(); String [][] data = new String [rowNum] [colNum]; for(int i = 0; i <rowNum; i++){ XSSFRow row = ws.getRow(i); for (int j = 0; j < colNum; j++){ XSSFCell cell = row.getCell(j); String value = cell.toString(); data[i][j] = value; System.out.println ("the value is " + value); } } 

inside excel file

 321312321321 321312321321 321312321321 

numbers with a length of 12 ++;

when I run the code, why does the string have such a format the value is 3.21312321321E11 , how can I get the correct format?

    1 answer 1

    The toString method only provides an object representation in a string form that may not meet your expectations; for conversion to a string in the desired form, it is better to write code with your hands, for example.

     public static void main(String... args) throws IOException { File excel = new File(args[0]); try (FileInputStream fis = new FileInputStream(excel); XSSFWorkbook wb = new XSSFWorkbook(fis);) { XSSFSheet ws = wb.getSheetAt(0); int rowNum = ws.getLastRowNum() + 1; int colNum = ws.getRow(0).getLastCellNum(); String[][] data = new String[rowNum][colNum]; NumberFormat nf = NumberFormat.getNumberInstance(); for (int i = 0; i < rowNum; i++) { XSSFRow row = ws.getRow(i); for (int j = 0; j < colNum; j++) { XSSFCell cell = row.getCell(j); String value = ""; String type = ""; switch (cell.getCellType()) { case CELL_TYPE_NUMERIC: value = nf.format(cell.getNumericCellValue()); type = "numeric"; break; case CELL_TYPE_STRING: value = cell.getStringCellValue(); type = "string"; } data[i][j] = value; System.out.println("the value is '" + value + "' with type '" + type + "'"); } } } } 

    The result of this code will be (on my machine)

     the value is '321 312 321 321' with type 'numeric' the value is '321 312 321 321' with type 'numeric' the value is '321 312 321 321' with type 'numeric' the value is '564 659 849 846' with type 'numeric' 

    All values ​​are represented as numbers, with a separator of digits in the form of a single space.