在java中读取Excel文件不会将空白单元格读取或计数为空

时间:2017-01-25 01:55:52

标签: java excel apache-poi

我在java中的excel阅读器上遇到了问题。它不会读取空单元格或空白单元格并跳到下一个具有值的单元格。

例如..

header1 | header2 | header3 | header4
row1    |         | row3    | row4
row1    | row2    |         | row4
        | row2    | row3    | 

结果:

header1 | header2 | header3 | header4
row1    | row3    | row4    | 
row1    | row2    | row4    | 
row2    | row3    |         | 

预计会发生:

header1 | header2 | header3 | header4
row1    | null    | row3    | row4
row1    | row2    | null    | row4
null    | row2    | row3    | null

null " " (空字符串)

这是我的代码:

我的 readXLSFile 方法:

private boolean readXLSFile(String batchRunNbr, String filename) throws IOException, ParseException {

        List sheetData = new ArrayList();
        FileInputStream fis = null;
        try {
            if ((filename.endsWith(".xlsx")) || (filename.endsWith(".XLSX"))) {
                log.info("Reading xlsx file...");
                fis = new FileInputStream(filename);
            } else{
                this.errorMessageTxt = this.errorMessageTxt+this.htmlNextLine+
                        "Unable to process the file. Please save the file to the latest Excel *.xlsm or *.xlsx file.";
                return true;
            }


            XSSFWorkbook workbook = new XSSFWorkbook(fis);
            XSSFSheet sheet = workbook.getSheetAt(0);

            Iterator<Row> rows = sheet.rowIterator();
            int counter = 0;
            while (rows.hasNext()) {
                counter++;
                XSSFRow row = ((XSSFRow) rows.next());
                Iterator<Cell> cells = row.cellIterator();

                List data = new ArrayList();
                while (cells.hasNext()) {
                    Cell cell = (XSSFCell) cells.next();

                    data.add(cell);
                }
                sheetData.add(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
            this.errorMessageTxt = this.errorMessageTxt+this.htmlNextLine+
                    e.getMessage()+this.htmlNextLine+e.getCause();
            return true; //There is an error, return true.
        } finally {
            if (fis != null) {
                fis.close();
            }
        }

        processExcelSheet(sheetData, batchRunNbr, filename);

这是 processExcelSheeet 方法:

private void processExcelSheet(List sheetData, String batchRunNbr, String inputFileName) {
        DateFormat formatter;
        formatter = new SimpleDateFormat("dd.MM.yyyy");
        boolean firstTime = true;
        try {
            for (int i = 0; i < sheetData.size(); i++) {
                List list =  (List) sheetData.get(i);
                if (firstTime) {
                    firstTime = false;
                } else {
                    for (int ii = 0; ii < list.size(); ii++) {

                        XSSFCell cell = (XSSFCell) list.get(ii);
                        switchCase(formatter, ii, cell);
                    }

                    insertToStaging(batchRunNbr, inputFileName);//This method inserts the data to the database based on what it reads above.
                }
                //log.info("COMPLETED!");
            }

        } catch (Exception e) {
            log.info("loadStagingTable Error:" + e);
            //this.errorMessageTxt = this.errorMessageTxt+this.htmlNextLine+
            //      "Error Loading to KLTL Data Staging. "+e.getMessage()+","+e.getCause();
            e.printStackTrace();
        }
    }

我怀疑问题出在 row.hasNext 上? 请帮忙..非常感谢你。如果您需要更多详细信息,请在下面做出评论。

1 个答案:

答案 0 :(得分:1)

到目前为止这个非常酷的代码!

只是一个疯狂的猜测:我打赌当你得到&#34; sheet.rowIterator()&#34;你失去了空位。这将是有意义的b / c&#34;迭代器&#34;经常忽略空值。

所以......如果是这样的......你如何解决它?

也许这会奏效:

定义&#34; sheet&#34>之后尝试做像

这样的事情
for(int i = 0; i<sheet.getLastRowNum(); i++){
  XSSFRow row = ((XSSFRow) sheet.getRow(i));
  for(int j = 0; j<row.getLastCellNum(); j++){
    String val = row.getCell(j).getStringCellValue();
    System.out.println(val.isEmpty()?"null":val );//todo: rewrite as readable "if" statement
  }
}

;)快乐的编码!