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

我正在制作一个程序,我从excel文件中读取数据并将它们存储在表中。 我使用Apache POI制作了程序并且工作正常。 但是当文件中有空白单元格时 在此处输入图像描述 我有一些问题。 程序跳过空白并读取下一个数据。 任何人都可以帮助我如何做到这一点? 我知道这个问题有几个post,但我没有找到对我有用的东西。

从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 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); 。 这可以帮我解决我的问题吗?

  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 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,因为下一个值为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添加到列表中。

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