2014-01-09 50 views
1

我需要幫助刪除創建的記錄列表之間的空行。我已經編碼刪除空行,但它只能刪除第一個空行。這裏是我的代碼完成:如何在使用POI的Excel中刪除行數據之間的空行使用Java的HSSF庫

enter image description here

結果:

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

它只能刪除空行

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

這裏附上也從上面的代碼導致結果如下,其餘的依然存在。

真的需要這方面的幫助。謝謝!

EMA

回答

0

我知道這是一個很老的線程,但今天早上就遇到了這個問題。不希望HSSF庫獲得空行。所以張貼這個答案,讓其他成員進入它將有一個答案。

這是我的解決方案 - 基本上覆制/粘貼發現的部分。

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

我的測試文件,此前6空行。他們都走了:)享受!

相關問題