当前位置: 首页 > news >正文

excel按模板文件导出多个文件并压缩为ZIP格式返回前端

思路:先准备好模板文件和与之对应的实体类,数据库数据等,还是之前思路,根据查出的数据,填充模板文件,生成一个临时文件,最后将这些个临时文件打包为zip返回前端,并将多个临时文件删除掉就ok了

紧接着就是copy

    @Log(title = "表格管理-批量导出", businessType = BusinessType.EXPORT)@Operation(description = "下载表格")@PostMapping("/imports")public void excelimport(HttpServletResponse response, @RequestBody List<FormVo> formVo) throws IOException {response.setContentType("application/zip");response.setCharacterEncoding("UTF-8");String fileName = "/home/ag/project/files/templates/" + System.currentTimeMillis() + ".zip";response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8"));FileOutputStream fos = new FileOutputStream(fileName);ZipOutputStream zos = new ZipOutputStream(fos);ExcelWriter writer = null;ArrayList<String> o = new ArrayList<>();for (FormVo f : formVo) {// 模板文件路径   switch case部分是我的业务代码(多个不同的模板文件)  各位就不用看了,删掉即可String templateFilePath;String type = f.getType();switch (type) {case "1":templateFilePath = form;break;case "2":templateFilePath = formCao;break;case "3":templateFilePath = formLin;break;case "4":templateFilePath = formYuan;break;case "5":templateFilePath = formShui;break;case "6":templateFilePath = formJian;break;case "7":templateFilePath = formQi;break;default:throw new RuntimeException("表格类型有误");}// 创建 ExcelWriter 实例String fileName1 = "/home/ag/project/files/templates/" + System.currentTimeMillis() + "听忆计划表.xlsx";writer = EasyExcel// 写入到临时文件.write(fileName1)// 指定模板.withTemplate(templateFilePath).build();WriteSheet sheet = EasyExcel.writerSheet().build();FillConfig fillConfig = FillConfig.builder()// 开启填充换行.forceNewRow(true).build();List list = getjituan1(f, f.getType());//计算公式Workbook workbook = writer.writeContext().writeWorkbookHolder().getWorkbook();workbook.setForceFormulaRecalculation(true);// 执行填充操作writer.fill(list, fillConfig, sheet);o.add(fileName1);
//            // 压缩文件writer.finish();}for (String f:o){// 压缩文件File file1 = new File(f);addFileToZip(file1, zos);
//            删除临时文件deleteFile(f);}zos.close();//将压缩文件输入流传给response输出流InputStream fileInputStream = new FileInputStream(fileName);OutputStream outputStream = response.getOutputStream();byte[] bytes = new byte[1024 * 8];int len;while ((len = fileInputStream.read(bytes)) != -1) {outputStream.write(bytes, 0, len);}outputStream.close();fileInputStream.close();}static void deleteFile(String path) {File file = new File(path);if (file.exists()) {file.delete();}}public static void addFileToZip(File file, ZipOutputStream zos) throws IOException {InputStream fileInputStream = new FileInputStream(file);//zip中要放文件称为zipEntryZipEntry zipEntry = new ZipEntry(file.getName());zos.putNextEntry(zipEntry);byte[] bytes = new byte[1024];int len;while ((len = fileInputStream.read(bytes)) > 0) {//读的内容会自动放到zip条目中,因此zipentry再输出流读完需要关闭zos.write(bytes, 0, len);}fileInputStream.close();zos.closeEntry();}

http://www.lryc.cn/news/380834.html

相关文章:

  • 自动驾驶仿真测试用例表格示例 ACC ELK FCW
  • 数组 (java)
  • 时序预测 | Matlab基于Transformer多变量时间序列多步预测
  • suuk-s.php.jpg-python 库劫持
  • python3GUI--ktv点歌软件By:PyQt5(附下载地址)
  • opencascade AIS_InteractiveContext源码学习2
  • scale()函数详解
  • 计算机基础学习有多重要?学哪些?如何学?
  • Oracle day9
  • Race Condition竞争条件
  • docker 删除本地镜像释放磁盘空间
  • JVM中的垃圾回收器
  • 记录一些可用的AI工具网站
  • vue3页面传参
  • QNX OS微内核系统
  • ViT:5 Knowledge Distillation
  • 2024头歌数据库期末综合(部分题)
  • 【Flask】学习
  • 图像数字化基础
  • 让你的Python代码更简洁:一篇文章带你了解Python列表推导式
  • 基于Matlab的BP神经网络的车牌识别系统(含GUI界面)【W7】
  • jetpack compose的@Preview和自定义主题
  • Temu(拼多多跨境电商) API接口:获取商品详情
  • ArcGIS Pro SDK (五)内容 2 工程项
  • 【ai】初识pytorch
  • pcl::PointXYZRGBA造成点云无法显示
  • 【论文精读】分类扩散模型:重振密度比估计(Revitalizing Density Ratio Estimation)
  • kubesphere踩过的坑,持续更新....
  • 做Android开发怎么才能不被淘汰?
  • 异步爬虫:aiohttp 异步请求库使用: