apache poi xlsx problem with format



  • There's 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

    12++;

    When the code starts, why the string has such a format the value is 3.21312321321E11 How can you get the right format?



  • Method toString It's only a structural presentation that can not match your expectations, so that you can change the line in the right form, you better write the 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 (in my car)

    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 presented as a number with a single flow divider.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2