Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

apache poi - How deal with blank cells in excel files java

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 enter image description 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.

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("");
        }

    }
}

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
            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);

YOUR METHOD:

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("");
        }

    }

Explanation:

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);

            }

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...