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

});

}

相关推荐
LinXunFeng6 小时前
Obsidian - 使用 Share Note 分享笔记并自部署
前端·笔记·github
乘风gg10 小时前
为什么AI 时代来临,大部分人吃不到红利
前端·ai编程·claude
恋猫de小郭10 小时前
Android 限制侧载新进展,谷歌联合国内厂商推验证计划
android·前端·flutter
IT_陈寒11 小时前
Redis内存爆了,原来我漏掉了这个致命配置
前端·人工智能·后端
恋猫de小郭11 小时前
解读 Android 17 全新内存限制,有没有“豁免”后门?
android·前端·flutter
小bo波11 小时前
从"任意文件复制"深挖Java I/O:字符流与字节流的本质抉择
java·nio·io流·后端开发·文件复制
Hyyy12 小时前
理解LLM的基本工作原理:预训练、微调、推理的区别
前端
Gatlin12 小时前
前端逆向与反逆向:一场猫鼠游戏的底层逻辑与实战
前端
Pedantic12 小时前
本地通知(Local Notifications)学习笔记
前端
森蓝情丶13 小时前
我给 AI 搭了个法庭:一个前端仔的 LangGraph 实战全记录
前端·后端