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 小时前
深度解密 Rollup 插件开发:核心钩子函数全生命周期图鉴
前端·vite
陳10301 天前
Linux:进程间切换与调度
linux·运维·服务器
java_nn1 天前
一文了解前端技术
前端
发现一只大呆瓜1 天前
深度解析 Rollup 配置与 Vite 生产构建流程
前端·vite
小码哥_常1 天前
安卓黑科技:让手机成为你的“跌倒保镖”
前端
小李子呢02111 天前
前端八股Vue---Vue2和Vue3的区别,set up的用法
前端·javascript·vue.js
m0_647057961 天前
Harness Engineering 实践指南
前端
JJay.1 天前
Android BLE 稳定连接的关键,不是扫描,而是 GATT 操作队列
android·服务器·前端
一 乐1 天前
电影院|基于springboot + vue电影院购票管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·电影院购票管理管理系统
星空椰1 天前
JavaScript 进阶基础:函数、作用域与常用技巧总结
开发语言·前端·javascript