如何使用Java中的POI HSSF库在Excel中的行数据之间移除空行

我需要帮助删除创build的logging列表之间的空行。 我已经编码删除空行,但它只能删除第一个空行。 这里是我做的代码:

private static void writeExcelFile(String[] keyValue, String fileName, String contents, ArrayList<String> listProperties, ArrayList<String> listPropertiesDescription, ArrayList<String> listPropertiesFileName) { int rownum = 2; HSSFSheet firstSheet; HSSFWorkbook workbook = null; workbook = new HSSFWorkbook(); firstSheet = workbook.createSheet("Resourcebundle"); Row headerRow = firstSheet.createRow((short) 1); headerRow.setHeightInPoints(30); headerRow.createCell((short) 0).setCellValue("Properties Name"); headerRow.createCell((short) 1).setCellValue("Properties Description"); headerRow.createCell((short) 2).setCellValue("Properties File Name"); System.out.println("listPropertiesDescription :: " + listPropertiesDescription.size()); System.out.println("listPropertiesFileName :: " + listPropertiesFileName.size()); System.out.println("listProperties all list :: " + listProperties.toString()); System.out.println("listPropertiesDescription all list :: " + listPropertiesDescription.toString()); int indexProperties = 0; for (int i = rownum; i < listProperties.size(); i++) { // Row row = firstSheet.getRow(i + 1); Row row = firstSheet.getRow(i); // System.out.println("row :: " + row); if (row == null) { // row = firstSheet.createRow(i + 1); row = firstSheet.createRow(i); } System.out.println("check index :: " + indexProperties); for (int j = 0; j < 1; j++) { Cell cell = row.getCell(j); System.out.println("cell :: " + cell); if (cell == null) { row.createCell(j).setCellValue( listProperties.get(indexProperties + 1).toString().trim()); row.createCell(j + 1).setCellValue( listPropertiesDescription.get(indexProperties + 1).toString().trim()); row.createCell(j + 2).setCellValue( listPropertiesFileName.get(indexProperties + 1).toString().trim()); } j++; } indexProperties++; System.out.println("check index below :: " + indexProperties); i++; } int lastRowCount = firstSheet.getLastRowNum(); for (int i = rownum; i < lastRowCount; i++) { HSSFRow row = firstSheet.getRow(i); if (row == null) { removeRow(firstSheet, i); // firstSheet.shiftRows(i + 1, lastRowCount, -1); // i--; // Since you move row at i+1 to i } } FileOutputStream fos = null; try { File file = new File("OnlineHelp Master Excel.xls"); //if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } String fileRB = outputLocation.concat("\\" + file); fos = new FileOutputStream(new File(fileRB)); workbook.write(fos); fos.close(); } catch (Exception e) { e.printStackTrace(); } } public static void removeRow(HSSFSheet sheet, int rowIndex) { int lastRowNum = sheet.getLastRowNum(); if (rowIndex >= 0 && rowIndex < lastRowNum) { sheet.shiftRows(rowIndex + 1, lastRowNum, -1); } if (rowIndex == lastRowNum) { HSSFRow removingRow = sheet.getRow(rowIndex); if (removingRow != null) { sheet.removeRow(removingRow); } } } 

这里我也附上上面的代码的结果:

在这里输入图像说明

结果:

 Row | Result 1 | result a 2 | (empty row) 3 | result b 4 | (empty row) 5 | result b 6 | (empty row) 

它只能删除空行下面的结果a,其余的还在那里。

真的需要帮助。 谢谢!

EMA

我知道这是一个相当古老的线程,但今天早上遇到了这个问题。 不希望HSSF库获得空行。 所以张贴这个答案,所以其他成员比跑到它将有一个答案。

这是我的解决scheme – 基本上复制/粘贴周围发现的部分。

  HSSFSheet sheet = new HSSFWorkbook(new ByteArrayInputStream(content)).getSheetAt(0); int headerRowIndex = sheet.getFirstRowNum(); List<String> columnNames = getColumnNames(sheet); List<Map<String, String>> sheetData = new ArrayList<>(); sheet.forEach(row -> { if (row.getRowNum() != headerRowIndex && !isRowEmpty(row)) { sheetData.add(getRowData(row, columnNames)); } }); 

…和方法:

  private boolean isRowEmpty(Row row) { for (int cellIndex = row.getFirstCellNum(); cellIndex < row.getLastCellNum(); cellIndex++) { Cell cell = row.getCell(cellIndex); if (cell != null && cell.getCellTypeEnum() != CellType.BLANK) { return false; } } return true; } 

我的testing文件以前有6个空行。 他们都走了:)享受!