首页 文章

如何使用Apache POI读取空的,但是格式化的Excel单元格?

提问于
浏览
3

我有一个使用Apache POI读取Excel单元格的方法,它工作正常 . 嗯......几乎没问题 .

public static ArrayList readXLsXFile() throws FileNotFoundException, IOException {

        ArrayList outListaExcel = new ArrayList();

        FileInputStream fis;
        ptxf= new FileInputStream(pathToExcelFile);
        XSSFWorkbook workbook = new XSSFWorkbook(ptxf);
        XSSFSheet sheetAr = workbook.getSheetAt(0);
        Iterator rowsAr = sheetAr.rowIterator();
        while (rowsAr.hasNext()) {
            XSSFRow row1 = (XSSFRow) rowsAr.next();
            Iterator cellsAr = row1.cellIterator();
            ArrayList<String> arr;
            arr = new ArrayList();
            while (cellsAr.hasNext()) {
                XSSFCell cell1 = (XSSFCell) cellsAr.next();
                arr.add(String.valueOf(cell1));
            }
            outListaExcel.add(arr);
        }
        return outListaExcel;
    }

如果格式化单元格,例如,如果整个A列具有边框,那么它将继续读取空单元格,从而为我提供空字符串 . 如何忽略那些空(格式化)细胞?

所以 readXLsXFile 会给我一个 ArryList

[0] -> [1][2]
[1] -> [3][4]

但是它还会给十个节点添加空字符串,因为coloumn A是用边框格式化的 .

edit 之后 Gagravarr 回答 .

我可以避免检查是否为 subList 为空,然后不将其添加到 mainList . 但是对于一些非常大的.xls文件,如果有很多这样的文件需要很长时间,而且我认为这不是一个好习惯 .

我的问题是,如果有 rows 的东西,就像我忽略了 cells 那样 .

ArrayList<ArrayList<String>>mainLista = new ArrayList<ArrayList<String>>();
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
        Row r = sheet.getRow(rowNum);
        int lastColumn = r.getLastCellNum();
        ArrayList<String> subList = new ArrayList<String>();
        for (int cn = 0; cn < lastColumn; cn++) {
            Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);

            if (c != null) {
                subList.add(c.getStringCellValue());
            } else {
            }
        }
        if (!subList.isEmpty() ){  // I think it is not good way
        mainLista.add(subList);}   // to do this, because it still reads 
    }                              // an empty rows

1 回答

  • 2

    正如Apache POI Documentation on Iterate over rows and cells中所解释的那样,迭代器只为您提供已定义且具有/具有内容的行和单元格 .

    如果要获取完全控制空白或空单元格的单元格,则需要使用以下内容:

    // Decide which rows to process
    int rowStart = Math.min(15, sheet.getFirstRowNum());
    int rowEnd = Math.max(1400, sheet.getLastRowNum());
    
    for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
       Row r = sheet.getRow(rowNum);
    
       int lastColumn = Math.max(r.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT);
    
       for (int cn = 0; cn < lastColumn; cn++) {
          Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
          if (c == null) {
             // The spreadsheet is empty in this cell
          } else {
             // Do something useful with the cell's contents
          }
       }
    }
    

    如果要获取空白单元格(通常是那些具有样式但没有值的单元格),请使用其他缺少单元格的策略,例如RETURN_NULL_AND_BLANK

相关问题