How deal with blank cells in excel files java

前端 未结 2 1721
我在风中等你
我在风中等你 2020-12-09 23:41

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 c

相关标签:
2条回答
  • 2020-12-10 00:37

    Another solution if you don't know the size of your spreadsheet is to loop through row and column and compare the index of row and column with the previous one you parsed. If the increment is more than one you will create the missing intermediate cells.

    0 讨论(0)
  • 2020-12-10 00:41
                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.

    0 讨论(0)
提交回复
热议问题