ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
EasyExcel.write(outputStream).withTemplate(filestream).sheet().doFill(templateMap);
byte[] data = outputStream.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
try {
// 将字节数组转换为 XSSFWorkbook
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
// 创建工作表
Sheet sheet = workbook.getSheet("模板");
// 获取公式求值器
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
// 计算所有公式
for (Row row : sheet) {
for (Cell cell : row) {
if (cell.getCellType() == CellType.FORMULA.getCode()) {
evaluator.evaluateInCell(cell);
}
}
}
// 写入响应体
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=yourFile.xlsx");
workbook.write(response.getOutputStream());
response.getOutputStream().flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
前端下载二进制文件
exportEleConsume(params: any) {
const url = /api/energy/report/exportYddhtjb?date=${params.date}&&deviceCategory=${params.deviceCategory}
;
this.http.request(‘GET’, url, { responseType: ‘blob’, observe: ‘response’ }).subscribe(res => {
const blob = new Blob([res.body!], { type: ‘application/octet-stream’ });
saveAs(blob, decodeURI(‘电能数据采集.xls’));
});
}