Home > Mobile >  Apache POI Blank cell enum isnt working properly?
Apache POI Blank cell enum isnt working properly?

Time:12-16

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

CodePudding user response:

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.

  • Related