简体   繁体   中英

Apache POI Blank cell enum isnt working properly?

I am trying to write the data from an excel file to a text file and if there is a blank cell I need to write "-" to the file. However this doesn't work on some excel files for reasons I do not understand.

Here's the code:

for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        sheet = wb.getSheetAt(i);
        fw = new FileWriter("C:\\Users\\Emre\\Desktop\\excelstore.txt");
        for (Row row : sheet) {
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                switch (cell.getCellType()) {
                    case STRING:
                        fw.write(cell.getStringCellValue() + ",");
                        break;
                    case NUMERIC:
                        fw.write(cell.getNumericCellValue() + ",");
                        break;
                    case BLANK:
                        fw.write("-" + ",");
                    default:
                }
            }
            fw.write("\n");

        }
        fw.close();

Pic of the excel file im supposed to read it from: https://ibb.co/2NqDgtj

Here is the output: https://ibb.co/MnMS0Dp

You cannot always rely on the CellType in your special case. It may happen that you have a cell with whitespaces in, identified as STRING. So check the content of cell.getStringCellValue() and replace by dash if empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM