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

});

}

相关推荐
郑州光合科技余经理21 分钟前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
骇客野人33 分钟前
测试环境MySQL迁移Vastbase(海量数据库)完整改造与落地实施方案
运维·服务器
+VX:Fegn08951 小时前
计算机毕业设计|基于springboot + vue旅游管理系统(源码+数据库+文档)
java·开发语言·vue.js·spring boot·课程设计
IT_陈寒1 小时前
Vue的响应式让我熬到凌晨三点,原来漏了这个小细节
前端·人工智能·后端
需要8261 小时前
MySQL 索引 B+ 树与“查询为什么变慢了
java·spring boot·spring·spring cloud·tomcat
一 乐1 小时前
二手交易平台|基于springboot + vue二手交易平台(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序
万年咸鱼1 小时前
编译Java源程序
java
Blanche15001 小时前
利用 RAG 为答疑机器人扩展知识范围
前端
天若有情6731 小时前
【纯前端小工具】公历生日转农历,批量查询每年农历生日对应的公历日期(GitHub Pages在线直接用)
前端·javascript·github pages·农历转换·lunisolar·网页小工具
SoonITer1 小时前
怎样构建一个 Agent-friendly 的网站
前端·agent