当用户尝试上传 (.xlsx) 文件时,我正在从 Excel 工作表中导入数据。对于一个特定字段 Field1 - 2233,它使用命名范围来获取实际值,而不是命名
当用户尝试上传 (.xlsx) 文件时,我正在从 Excel 工作表中导入数据。对于一个特定字段 Field1 - 2233,它使用命名范围来获取实际值,而不是命名范围。
命名范围:
"IF(AND('Sheet 1'!NbFCbag <>\"Weight including baggage\",'Sheet 1'!NbCCbag <> \"Weight including baggage\"),('Sheet 1'!NbFC*'Sheet 1'!WFC + 'Sheet 1'!NbFCbag*'Sheet 1'!WFCbag) + ('Sheet 1'!NbCC*'Sheet 1'!WCC + 'Sheet 1'!NbCCbag*'Sheet 1'!WCCbag),IF(AND('Sheet 1'!NbFCbag<>\"Weight including baggage\",'Sheet 1'!NbCCbag = \"Weight including baggage\"),('Sheet 1'!NbFC*'Sheet 1'!WFC + 'Sheet 1'!NbFCbag*'Sheet 1'!WFCbag) + 'Sheet 1'!NbCC*'Sheet 1'!WCC,IF(AND('Sheet 1'!NbFCbag=\"Weight including baggage\",'Sheet 1'!NbCCbag <> \"Weight including baggage\"),'Sheet 1'!NbFC*'Sheet 1'!WFC + ('Sheet 1'!NbCC*'Sheet 1'!WCC + 'Sheet 1'!NbCCbag*'Sheet 1'!WCCbag),'Sheet 1'!NbFC*'Sheet 1'!WFC+ 'Sheet 1'!NbCC*'Sheet 1'!WCC)))"
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
for (Name namedRange : workbook.getAllNames()) {
String rangeName = namedRange.getNameName();
try {
if (NAMED_RANGE.contains(rangeName)) {
AreaReference areaReference = new AreaReference(namedRange.getRefersToFormula(), oweWorkbook.getSpreadsheetVersion());
CellReference[] cellReferences = areaReference.getAllReferencedCells();
for (CellReference cellReference : cellReferences) {
Sheet namedSheet = oweWorkbook.getSheet(cellReference.getSheetName());
Row row = namedSheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getCol());
if (cell != null) {
namedRangeValues.put(rangeName, getFormulaCellValueAsString(cell,evaluator));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private String getFormulaCellValueAsString(Cell cell, FormulaEvaluator evaluator) {
CellValue cellValue = evaluator.evaluate(cell);
logger.info("cellValue:");
switch (cellValue.getCellType()) {
case STRING:
return cellValue.getStringValue();
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
return cell.getDateCellValue().toString();
} else {
return Double.toString(cellValue.getNumberValue());
}
case BOOLEAN:
return Boolean.toString(cellValue.getBooleanValue());
default:
return "";
}
}
我该如何解码才能获得准确的值?
我尝试使用 Formula Evaluator
但失败并出现错误: Second part of cell reference expected after sheet name at index
.