如何处理excel文件中的空白单元格java [英] How deal with blank cells in excel files java

查看:38
本文介绍了如何处理excel文件中的空白单元格java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个程序,我从 Excel 文件中读取数据并将它们存储在表格中.我已经使用 Apache POI 制作了程序并且运行良好.但是当文件有空白单元格时 我有一些问题.程序跳过空白并读取下一个数据.谁能帮助我如何做到这一点?我知道这个问题有几个帖子,但我没有找到对我有用的东西.

I am making a program where I am reading data from excel files and store them in tables. I have made the program using Apache POI and works fine. But when files have blank cells as the one here I have some problems. The program skip the blanks and read the next data. Could anyone help me how I would do it? I know that there are several posts for this issue but I have not found something useful for me.

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

The code for reading the data from excel file is the below. As you can see I have 3 types of data. How i would give the option for 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); 的内容.这能帮我解决问题吗?

Also I have read about workbook.setMissingCellPolicy(HSSFRow.RETURN_NULL_AND_BLANK);. Could this help me with my problem?

推荐答案

            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,因为下一个值为空.

int maxNumOfCells = sheet.getRow(0).getLastCellNum(); - This line will make sure that you were able to get the number of columns. Using the Row's method .getLastCellNum() on the next rows will result to unexpected number. Example on your on row 3 of your spreadsheet, the method will return 2 since the next value is null.

            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 添加到您的列表中.

Looping through the cells. From cell 0 (Base 0) to the last cell number. If the cell was found null, basically, it would create the cell with a blank value. Lastly, adding the cell to your List.

这篇关于如何处理excel文件中的空白单元格java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆