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

});

}

相关推荐
python在学ing2 分钟前
前端-CSS学习笔记
前端·css·python·学习
nJI74egg13 分钟前
JavaEE初阶---《JUC 并发编程完全指南:组件用法、原理剖析与面试应答》
java·面试·java-ee
匆匆那年96710 分钟前
远程 Linux 校园网认证操作手册(本地浏览器法)
linux·运维·服务器
刮风那天11 分钟前
Android AMS创建进程不用Binder而用Socket?
android·java·binder
Bug-制造者15 分钟前
【Vue3 实战】全局错误处理体系搭建:实现业务与错误彻底解耦
前端·javascript·vue.js
程序员老邢17 分钟前
【技术底稿 37】Spring Boot 3.x 自动装配 “死锁” 排查:3 个注解实现条件化装配与 Mock 兜底
java·spring boot·后端·自动装配·rag·技术底稿
悟空瞎说18 分钟前
# Git 交互式变基:优雅整理提交历史,告别杂乱 PR 记录
前端·git
还有多久拿退休金30 分钟前
DragSortTable:一个让我怀疑人生的滚动重置 Bug
前端
日月云棠32 分钟前
JAVA数据结构与算法 - 基础:链表
java·后端
渐儿33 分钟前
组件库开发入门到生产(从零封装到 npm 发布)
前端