easyexcel模板导出并且公式自动计算的方法

复制代码
	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'));

});

}

相关推荐
alonewolf_998 分钟前
深入解析G1与ZGC垃圾收集器:原理、调优与选型指南
java·jvm·算法
小镇学者10 分钟前
【c++】C++字符串删除末尾字符的三种实现方法
java·开发语言·c++
rfidunion11 分钟前
springboot+VUE+部署(1。新建项目)
java·vue.js·spring boot
小翰子_11 分钟前
Spring Boot整合Sharding-JDBC实现日志表按月按周分表实战
java·spring boot·后端
weixin_3993806919 分钟前
OA 系统假死问题分析与优化
java·运维
wuk99827 分钟前
梁非线性动力学方程MATLAB编程实现
前端·javascript·matlab
济61732 分钟前
linux(第十四期)--Uboot移植(2)-- 在U-Boot 中添加自己的开发板-- Ubuntu20.04
linux·运维·服务器
ben9518chen36 分钟前
嵌入式linux操作系统简介
linux·运维·服务器
XiaoYu200238 分钟前
第11章 LangChain
前端·javascript·langchain
豆沙沙包?39 分钟前
2026年--Lc334-2130. 链表最大孪生和(链表转数组)--java版
java·数据结构·链表