java编程:我在使用easypoi 模板导出时候,单元格公式不生效,如单元格公式 =SUM(D10:D12), 已经有值了,但是还是没有自动计算,网上各种办法已经尝试了无效:
已解决:
1.如果你在使用easypoi进行模板填充导出时候公式失效,且已经尝试了网上各种办法依然无效,
2.我的原因是:在easypoi进行填充时候,都是使用字符串进行填充,即使你看到单元格显示是纯数字,其实它还是字符串类型
3.我的解决方案,自定义 Workbook的获取方式,数据填充时候数字类型区分开,EasyPoiUtil是我自定义的二次封装工具类,ExcelTemplateEnum 也是我自定义的模板信息枚举
代码如下:
/**
* 自定义模板 workbook获取, 填充时候会区分数字类型,开启公式自动计算
*
* @param excelTemplateEnum 模板枚举
* @param excelValueMap 待填充参数
* @return
*/
public static Workbook createWorkbookCustomer(Map<String, Object> excelValueMap, ExcelTemplateEnum excelTemplateEnum) {
try (InputStream is = EasyPoiUtil.class.getClassLoader().getResourceAsStream(excelTemplateEnum.getPath());
BufferedInputStream fis = new BufferedInputStream(is)) {
Workbook workbook = new XSSFWorkbook(fis);
Sheet sheet = workbook.getSheetAt(0);
Set<String> keys = excelValueMap.keySet();
for (Row row : sheet) {
for (Cell cell : row) {
if (cell == null) {
continue;
}
if (cell.getCellType() == CellType.STRING) {
String cellValue = cell.getStringCellValue();
if (cellValue != null && !cellValue.isEmpty()) {
for (String key : keys) {
String pattern = "\\{\\{" + key + "\\}\\}";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(cellValue);
if (m.find()) {
Object value = excelValueMap.get(key);
cell.setCellValue(cellValue.toString());
if (ObjectUtil.isEmpty(value)) {
cellValue = m.replaceAll("");
cell.setCellValue(cellValue);
continue;
}
//就是这里需要区分数字和其他类型
if (value instanceof Number) {
cell.setCellValue(((Number) value).doubleValue());
} else {
cellValue = m.replaceAll(value.toString());
cell.setCellValue(cellValue);
}
}
}
}
}
}
}
workbook.setForceFormulaRecalculation(true); // 强制公式自动计算
return workbook;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
我原来的方式是:
/**
* 获取工作簿对象
*
* @param excelValueMap
* @throws Exception
*/
public static Workbook createWorkbook(Map<String, Object> excelValueMap, ExcelTemplateEnum excelTemplateEnum) {
//1.获取模板信息对象
TemplateExportParams params = new TemplateExportParams(excelTemplateEnum.getPath());
//2.创建工作簿对象
Workbook workbook = ExcelExportUtil.exportExcel(params, excelValueMap);
return workbook;
}
代码贴出来
easypoi在导出时没有触发Excel的公式计算功能,导致单元格显示的是公式本身,而不是计算结果
我怀疑是填充数据后,excel将数据识别为字符串类型,所以不去计算
【以下回答由 GPT 生成】
首先,需要检查以下几个方面: 1. 确保使用的easypoi版本是最新的,以便获取最新的功能和修复的bug。 2. 确保单元格的格式设置为数值格式,而不是文本格式。在Excel中,公式只会对数值格式的单元格生效。 3. 确保使用的Excel软件支持计算公式,比如Microsoft Office Excel。 4. 确保使用的公式语法是正确的。在Excel中,公式应该以等号 "=" 开头。 5. 如果公式中包含引用其他工作簿中的单元格,需要确保这些工作簿是打开的或者链接的。
如果以上都是正确的,但公式仍然不生效,您可以尝试以下方法:
Workbook.setForceFormulaRecalculation(true)
方法来实现。Workbook workbook = ExcelExportUtil.exportExcel(exportParams, entityList);
workbook.setForceFormulaRecalculation(true);
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, entityList);
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Sheet sheet = workbook.getSheetAt(0);
Cell cell = sheet.getRow(rowNum).getCell(cellNum);
evaluator.evaluateFormulaCell(cell);
上述代码中的rowNum
和cellNum
是公式所在单元格的行号和列号。
如果以上方法都无效,可能是由于easypoi对某些特定的公式语法或功能支持不完全导致的。在这种情况下,建议您尝试其他的Excel导出工具或手动编写代码来实现需求。