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

});

}

相关推荐
ITresearchGuest12 分钟前
LangChain JS 入门:快速搭建前端 AI 开发环境
前端·javascript·langchain
智恒百亿12 分钟前
从开箱到跑通大模型:一台 RTX 5090 八卡服务器的部署实录(附踩坑清单)
运维·服务器
沫璃染墨15 分钟前
《从零入门Linux系统篇(三十二):文件篇·五——深入理解磁盘:从物理结构到LBA寻址》
linux·运维·服务器·系统架构·硬件架构
YHHLAI15 分钟前
React + TypeScript 实战:从零构建颜色选择器应用
前端·react.js·typescript
Profile排查笔记23 分钟前
指纹浏览器手机版怎么选?从本地 App 到云端 Android 的实现方式解析
前端·人工智能·后端·自动化
2601_9620652524 分钟前
2.启动mysql容器
java·数据库·mysql
2601_9620654925 分钟前
PHP For 循环
android·java·php
Songxwn26 分钟前
Rack-auto 裸金属服务器自动化
运维·服务器·自动化
2601_9622017232 分钟前
Java进阶,集合,Colllection,常见数据结构
java·数据结构·windows
zlwool32 分钟前
非标设备厂图纸管理选型实战
运维·服务器·erp·设备erp·非标机械