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

});

}

相关推荐
L-影21 分钟前
HashMap详解-上篇
java·开发语言·spring·hashmap
爱笑的源码基地26 分钟前
什么是智能预问诊系统?一套java版预问诊成品源码
java·源码·程序·预问诊·智能问诊·预问诊源码
workflower43 分钟前
如何培养FDE-3
前端·网络·安全·机器人·自动化
她说..1 小时前
幂等问题分析
java·jvm·spring boot·spring·spring cloud·intellij-idea
摇滚侠1 小时前
《On Java 中文版 基础卷》阅读笔记 集合 24
java·开发语言·笔记
小小测试开发1 小时前
rsync 源路径少一个斜杠,--delete 删的就不是你以为的那一层
java·前端·数据库·人工智能
智购科技无人售货机工厂店2 小时前
自动售货机刷脸支付集成实战:活体检测与合规门槛~YH
java·服务器·前端·科技·零售
Nazarick3 小时前
力扣LeetCode热题75题(1)
java·算法·leetcode·力扣
黑色的白兔No13 小时前
若依平台cloud版本搭建
java·spring cloud·微服务·nacos·vue·若依
开开心心就好3 小时前
定时结束进程的小工具,到点还能关机
linux·运维·服务器·智能手机·pdf·电脑·word