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

});

}

相关推荐
hoLzwEge几秒前
团队协作的隐藏利器:.vscode 完全指南
前端·前端框架
labixiong2 分钟前
TypeScript 7.0 编译器用 Go 重写,速度暴增10倍——背后到底做了什么?
前端·javascript·go
zx1154504 分钟前
Java 反射 (SpringBoot篇)
java·架构
WaywardOne4 分钟前
Flutter组件化方案(AI总结)
前端·flutter·ai编程
玉宇夕落5 分钟前
原子化编程”:Tailwind CSS 核心原理
前端
2301_794461576 分钟前
Activiti/BPMN 2.0 的 4 种网关
java·服务器·开发语言
她的男孩7 分钟前
低代码只能做单表 CRUD?我们一行代码没写,搭了个完整进销存
java·后端·架构
是小李呀10 分钟前
解决“Error: The project seems to require yarn but it‘s not installed”报错
前端
weedsfly11 分钟前
一个电商价格计算案例,带你学会前端开发中的责任链模式
前端·javascript·面试
__sjfzllv___11 分钟前
在职前端Leader学习/转行 AI Agent -DAY10
前端