简体   繁体   English

Apache POI 空白单元格枚举无法正常工作?

[英]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.我正在尝试将 excel 文件中的数据写入文本文件,如果有空白单元格,我需要在文件中写入“-”。 However this doesn't work on some excel files for reasons I do not understand.但是,由于我不明白的原因,这不适用于某些 excel 文件。

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我应该从以下位置读取的 excel 文件的图片: https ://ibb.co/2NqDgtj

Here is the output: https://ibb.co/MnMS0Dp这是输出: https ://ibb.co/MnMS0Dp

You cannot always rely on the CellType in your special case.在您的特殊情况下,您不能总是依赖 CellType。 It may happen that you have a cell with whitespaces in, identified as STRING.您可能会遇到一个带有空格的单元格,标识为 STRING。 So check the content of cell.getStringCellValue() and replace by dash if empty.因此,检查 cell.getStringCellValue() 的内容,如果为空则用破折号替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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