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'));

});

}

相关推荐
霸道流氓气质几秒前
Maven 批处理脚本与 Qoder 配置使用指南
java·maven
架构源启2 分钟前
Spring AI进阶系列(14)- 2026 可观测性最佳实践:从链路追踪到企业级 AI 治理落地
java·人工智能·spring
神奇的代码在哪里3 分钟前
【单机离线版】大学考试题库复习工具:前端离线Excel解析 + localStorage持久化 + Playwright
前端·html·ai编程·题库复习·刷题软件·大学考试
IOT.FIVE.NO.15 分钟前
Claude code+Vscode+Remote ssh+ 服务器自定义第三方API配置保姆级教程
服务器·vscode·ssh
饿了吃洗衣凝珠5 分钟前
【无标题】
运维·服务器·网络
码上有光7 分钟前
c++: 继承(下)
android·java·c++·多继承·菱形继承·虚继承
JAVA9657 分钟前
JAVA面试-并发篇 02-synchronized 锁可以重入吗
java·面试
daols888 分钟前
vxe-table 实现数据分组统计与表尾合计
前端·javascript·vue.js·vxe-table
向日的葵00610 分钟前
Vue 函数定义、事件绑定与列表渲染精讲
前端·javascript·vue.js
染翰12 分钟前
Linux 配置:应用用户执行 sudo su root 免密(运维标准配置)
linux·运维·服务器