POI XSSF 读取所有列而不仅仅是所有定义的列 - 结果不是预期的

POI XSSF reading all columns not just all defined columns - results not expected

有没有办法不读取整个 Excel 文档行,我正在读取文档中定义的单元格,但是,它正在拉入整个 sheet 的列? ?

我正在将 Excel 文档转换为 CSV 文档。我得到了这个结果。

Aircraft ID (FADEC_SHIP_POS),Fleet (Fleet Name),Fault Date (TIMESTAMP or FADEC_DATE_TIME),Fault ID (FADEC_ID),Fault Description (FADEC_FAULT_DESCRIPTION),Priority (FADEC_PRIORITY),ATA,Flight Phase (FM),Report Type (REPORT_TYPE),Flight Number (FLT),Origin (ORIG),Destination (DEST),Site Reference Name,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

我不需要那些额外的未定义列,(, ,)

此外,该行定义的列中可以有空值:- 这些是预期和需要的

6808-1,B757,2019-09-25 20:50:18,351-21A,P(S) 351-21,3,,Climb,901,1278,,,Sprint Airlines

我的class

public class XlsxToCsv implements Processor {

private Logger log = Logger.getLogger(XlsxToCsv.class.getName());

 private static final String CSV_SEPERATOR_CHAR=",";
 private static final String NEW_LINE_CHARACTER="\r\n";

@Override
public void process(Exchange exchange) throws Exception {

    log.info("Entering XLSX Excel to CSV ...");

    int sheetIdx = 0;
    byte[] in = exchange.getIn().getBody(byte[].class);
    String out = excelToCSV(in,sheetIdx);
    exchange.getIn().setBody(out);

    log.info("Exiting XLSX Excel to CSV ...");
}

private String excelToCSV(byte[] xlsxFile, int sheetIdx) throws Exception {

    InputStream is = null;
    StringBuffer sb = new StringBuffer();
    String cellVal = "";

    try {
        is = new ByteArrayInputStream(xlsxFile);
    } catch (Exception e) {
        e.printStackTrace();
    } 

    XSSFWorkbook workBook = new XSSFWorkbook(is);
    XSSFSheet workSheet = workBook.getSheetAt(sheetIdx);

    for (int i = 0; i < workSheet.getPhysicalNumberOfRows(); i++) {
        XSSFRow row = workSheet.getRow(i);
        for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
            cellVal = getCellValueAsString(workBook, row.getCell(j));
                if ( j == row.getPhysicalNumberOfCells() - 1) {
                    sb.append(cellVal).append(NEW_LINE_CHARACTER);
                } else {
                    sb.append(cellVal).append(CSV_SEPERATOR_CHAR);
                }
        }
    }
    workBook.close();
    return sb.toString();
}
    private String getCellValueAsString(XSSFWorkbook workbook, XSSFCell cell) {
        String cellValue = "";
        if (cell != null) {
            CellType cellType = cell.getCellType();
            if (cellType == CellType.FORMULA) {
                FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
                cellType = evaluator.evaluateFormulaCell(cell);
            }
            switch (cellType) {
                case STRING:
                    cellValue = cell.getStringCellValue();
                    break;
                case NUMERIC:
                    Double doubleValue = cell.getNumericCellValue();
                    int intValue = doubleValue.intValue();
                    cellValue = Integer.toString(intValue);
                    break;
                case BOOLEAN:
                    cellValue = Boolean.toString(cell.getBooleanCellValue());
                    break;
                case ERROR:
                    cellValue = cell.getErrorCellString();
                    break;
                case BLANK:
                    break;
                default:
                    break;
            }
        }

        return cellValue;
    }

}

事实证明,我使用的模板在电子表格的最后一个元素中输入了空格,我删除并清理了 xlsx 模板,它按预期工作。

一直在学习,我不希望那样,但是,它就是这样,