gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:13:21 35 4
gpt4 key购买 nike

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

我正在将 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 模板,它按预期工作。

总是在学习,我没想到会这样,但是,事情就是这样,

关于java - POI XSSF 读取所有列而不仅仅是所有定义的列 - 结果不是预期的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58107135/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com