easyExcel和Apache Poi的结合 ,给导出模板加密

import org.apache.poi.poifs.crypt.EncryptionInfo; import org.apache.poi.poifs.crypt.EncryptionMode; import org.apache.poi.poifs.crypt.Encryptor; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.util.List; import java.util.Map; public class ExcelUtil { public static void exportExcel(HttpServletRequest request, HttpServletResponse response, String name, List<?> list, Map<?, Object> map, String teName) throws Exception { // 使用EasyExcel将数据写入到一个临时文件中 File tempFile = File.createTempFile("temp", ".xlsx"); try (OutputStream tempOut = new FileOutputStream(tempFile)) { InputStream templateFileName = ExcelUtil.class.getClassLoader().getResourceAsStream(teName); ExcelWriter excelWriter = EasyExcel.write(tempOut).withTemplate(templateFileName).build(); WriteSheet sheet = EasyExcel.writerSheet().build(); FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); if (list != null && list.size() != 0) excelWriter.fill(list, fillConfig, sheet); if (map != null) excelWriter.fill(map, sheet); excelWriter.finish(); } // 使用Apache POI读取临时文件并加密数据 Workbook workbook = WorkbookFactory.create(tempFile); POIFSFileSystem fs = new POIFSFileSystem(); EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile); Encryptor enc = info.getEncryptor(); enc.confirmPassword("123456"); // 设置密码 try (OutputStream os = enc.getDataStream(fs)) { workbook.write(os); } // 设置响应头,以便下载 String fileName = name + System.currentTimeMillis() + ".xlsx"; DownLoadHelper.setDownloadHeader(response, request, fileName); // 将加密后的数据写入到客户端的输出流 try (OutputStream out = response.getOutputStream()) { fs.writeFilesystem(out); } // 删除临时文件 tempFile.delete(); } } // 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值