首页 > 解决方案 > 无法使用 HSSF 忽略空行

问题描述

下面的方法写来从 excel 中获取所有数据,但这也是选择空行。您能否建议更改以忽略空行?

public ArrayList<Map<String, String>> getCompleteSheetExcelData(String workbookName, String sheetName) throws IOException {
    //obtaining input bytes from a file
    FileInputStream fis=new FileInputStream(new File(excelpath+workbookName+".xls"));
    //creating workbook instance that refers to .xls file
    HSSFWorkbook wb=new HSSFWorkbook(fis);
    //creating a Sheet object to retrieve the object
    HSSFSheet sheets=wb.getSheet(sheetName);
    FormulaEvaluator formulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) wb);
    wbname = workbookName;
    sName = sheetName;
    ArrayList<Map<String, String>> excelData = new ArrayList<>();
    DataFormatter formatter = new DataFormatter();
    int rowCount = sheets.getLastRowNum();
    for(int i=1;i<=rowCount;i++){
        Row row = sheets.getRow(0);
        Map<String, String> rowData = new HashMap<>();
        for (int j = 0; j < row.getLastCellNum(); j++) {
            String key = row.getCell(j).getStringCellValue().trim();
            try {
                String value = formatter.formatCellValue(sheets.getRow(i).getCell(j),formulaEvaluator);
                rowData.put(key, value);
            }
            catch (NullPointerException e){
                rowData.put(key, "");
            }
        }
        excelData.add(rowData);
    }
    fis.close();
    return excelData;
}

标签: javahssf

解决方案


推荐阅读