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

});

}

相关推荐
Elias不吃糖2 小时前
Java Lambda 表达式
java·开发语言·学习
尾善爱看海2 小时前
不常用的浏览器 API —— Web Speech
前端
情缘晓梦.3 小时前
C语言指针进阶
java·开发语言·算法
美酒没故事°3 小时前
vue3拖拽+粘贴的综合上传器
前端·javascript·typescript
南知意-4 小时前
IDEA 2025.3 版本安装指南(完整图文教程)
java·intellij-idea·开发工具·idea安装
北邮刘老师4 小时前
A3C Network:智能体互联网的层次化视图
运维·服务器·网络
XRJ040618xrj4 小时前
如何在Linux中根据物理网卡建立虚拟网卡
linux·服务器·网络
jingling5554 小时前
css进阶 | 实现罐子中的水流搅拌效果
前端·css
空中楼阁,梦幻泡影4 小时前
LoRA 详细解析,使用LoRA 方式对模型进行微调详细操作指南
运维·服务器·人工智能·机器学习·语言模型
码农水水5 小时前
蚂蚁Java面试被问:混沌工程在分布式系统中的应用
java·linux·开发语言·面试·职场和发展·php