如何处理excel文件中的空白单元格java

时间:2013-05-17 08:33:39

标签: java apache-poi

我正在创建一个程序,我从excel文件中读取数据并将它们存储在表中。我使用Apache POI制作了程序并且工作正常。但是当文件中的空白单元格为enter image description here时,我遇到了一些问题。程序跳过空白并读取下一个数据。任何人都可以帮助我如何做到这一点?我知道这个问题有几个帖子,但我没有找到对我有用的东西。

从excel文件中读取数据的代码如下。如您所见,我有3种类型的数据。我将如何为BLANK CELL提供选项?

// Create an ArrayList to store the data read from excel sheet.
        List sheetData = new ArrayList();
        FileInputStream fis = null;
        try {
            // Create a FileInputStream that will be use to read the
            // excel file.
            fis = new FileInputStream(strfullPath);
            // Create an excel workbook from the file system
            HSSFWorkbook workbook = new HSSFWorkbook(fis);

            // Get the first sheet on the workbook.
            HSSFSheet sheet = workbook.getSheetAt(0);

            // store the data read on an ArrayList so that we can printed the
            // content of the excel to the console.
            Iterator rows = sheet.rowIterator();
            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow) rows.next();
                Iterator cells = row.cellIterator();

                List data = new ArrayList();
                while (cells.hasNext()) {
                    HSSFCell cell = (HSSFCell) cells.next();
                    data.add(cell);
                }
                sheetData.add(data);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
showExcelData(sheetData);
}
private static void showExcelData(List sheetData) {
        // LinkedHashMap<String, String> tableFields = new LinkedHashMap();
        for (int i = 0; i < sheetData.size(); i++) {
            List list = (List) sheetData.get(i);
            for (int j = 0; j < list.size(); j++) {
                Cell cell = (Cell) list.get(j);
                if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    System.out.print(cell.getNumericCellValue());
                } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    System.out.print(cell.getRichStringCellValue());
                } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                    System.out.print(cell.getBooleanCellValue());
                } else if (cell.getCellType()== Cell.CELL_TYPE_BLANK ){
                    System.out.print(cell.toString());
                }
                if (j < list.size() - 1) {
                    System.out.print(", ");
                }
            }
            System.out.println("");
        }

    }
}

我也读过关于workbook.setMissingCellPolicy(HSSFRow.RETURN_NULL_AND_BLANK);的文章。这有助于解决我的问题吗?

2 个答案:

答案 0 :(得分:6)

            int maxNumOfCells = sheet.getRow(0).getLastCellNum(); // The the maximum number of columns
            Iterator rows = sheet.rowIterator();
            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow) rows.next();
                Iterator cells = row.cellIterator();

                List data = new ArrayList();
                for( int cellCounter = 0
                        ; cellCounter < maxNumOfCells
                        ; cellCounter ++){ // Loop through cells

                    HSSFCell cell;

                    if( row.getCell(cellCounter ) == null ){
                        cell = row.createCell(cellCounter);
                    } else {
                        cell = row.getCell(cellCounter);
                    }

                    data.add(cell);

                }

                sheetData.add(data);

您的方法:

public static void showExcelData(List sheetData) {

        // LinkedHashMap<String, String> tableFields = new LinkedHashMap();
        for (int i = 0; i < sheetData.size(); i++) {
            List list = (List) sheetData.get(i);
            for (int j = 0; j < list.size(); j++) {
                Cell cell = (Cell) list.get(j);
                if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    System.out.print(cell.getNumericCellValue());
                } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    System.out.print(cell.getRichStringCellValue());
                } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                    System.out.print(cell.getBooleanCellValue());
                } else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
                    System.out.print("THIS IS BLANK");
                }
                if (j < list.size() - 1) {
                    System.out.print(", ");
                }
            }
            System.out.println("");
        }

    }

说明:

int maxNumOfCells = sheet.getRow(0).getLastCellNum(); - 此行将确保您能够获得列数。在下一行上使用Row的方法.getLastCellNum()将导致意外的数字。在电子表格第3行的示例中,该方法将返回2,因为下一个值为空。

            for( int cellCounter = 0
                    ; cellCounter < maxNumOfCells
                    ; cellCounter ++){ // Loop through cells

                HSSFCell cell;

                if( row.getCell(cellCounter ) == null ){
                    cell = row.createCell(cellCounter);
                } else {
                    cell = row.getCell(cellCounter);
                }

                data.add(cell);

            }

循环细胞。从单元格0(基数0)到最后一个单元格编号。如果找到了单元格null,基本上,它会创建一个blank值的单元格。最后,将cell添加到您的列表中。

答案 1 :(得分:0)

如果您不知道电子表格的大小,另一种解决方案是循环遍历行和列,并将行和列的索引与您解析的前一个索引进行比较。如果增量大于1,您将创建缺少的中间单元格。