设为首页收藏本站
网站公告 | 这是第一条公告
     

 找回密码
 立即注册
缓存时间01 现在时间01 缓存数据 当你走完一段之后回头看,你会发现,那些真正能被记得的事真的是没有多少,真正无法忘记的人屈指可数,真正有趣的日子不过是那么一些,而真正需要害怕的也是寥寥无几。

当你走完一段之后回头看,你会发现,那些真正能被记得的事真的是没有多少,真正无法忘记的人屈指可数,真正有趣的日子不过是那么一些,而真正需要害怕的也是寥寥无几。

查看: 1067|回复: 2

Java如何导出多个excel并打包压缩成.zip文件

[复制链接]

  离线 

TA的专栏

  • 打卡等级:即来则安
  • 打卡总天数:16
  • 打卡月天数:0
  • 打卡总奖励:252
  • 最近打卡:2023-12-29 08:30:22
等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
39
主题
33
精华
0
金钱
373
积分
84
注册时间
2023-8-12
最后登录
2025-6-1

发表于 2024-9-27 18:11:14 | 显示全部楼层 |阅读模式
目录
  • Java导出多个excel并打包压缩成.zip文件
    • 1、先获取到数据
    • 2、将导出的数据转成多个excel
    • 3、相关工具类
  • 总结

    Java导出多个excel并打包压缩成.zip文件

    1、先获取到数据

    并将数据导出excel到指定位置

    1. public void downPoliceZip(WorksitePoliceApiInfo worksitePoliceApiInfo) throws Exception {
    2. String zipName = "同步数据" + LocalDate.now() ;
    3. List<Map<String, Object>> workerMaps = new LinkedList<>();
    4. List<Map<String, Object>> worksiteMaps = new LinkedList<>();
    5. Map<String,Object > map = new LinkedHashMap<>();
    6. //获取数据
    7. .........
    8. // String realPath = request.getSession().getServletContext().getContextPath();
    9. //创建临时文件夹保存excel
    10. String tempDir = zipPath + "/tempDir/" + LocalDate.now() + "/";
    11. //将导出的数据转成多个excel
    12. List<File> files = this.getStoreOrderExcels(tempDir, workerMaps, worksiteMaps, flag);
    13. //下载zip
    14. boolean tag = FileDownloadUtils.generateFile(tempDir, "zip", zipPath, zipName);
    15. //删除tempDir文件夹和其中的excel和zip文件
    16. boolean b = FileDownloadUtils.deleteDir(new File(zipPath + "\\tempDir"));
    17. if (!b) {
    18. throw new RuntimeException("tempDir文件夹及其中的临时Excel和zip文件删除失败");
    19. }
    20. }
    复制代码

    2、将导出的数据转成多个excel

    1. /**
    2. * 将导出的数据转成多个excel
    3. *
    4. * @param tempDir 路径
    5. * @param workerMaps map集合
    6. * @param worksiteMaps map集合
    7. * @param flag 标识
    8. * @return List<File>
    9. * @throws IOException 异常
    10. */
    11. private List<File> getStoreOrderExcels(String tempDir, List<Map<String, Object>> workerMaps, List<Map<String, Object>> worksiteMaps, String[] flag) throws IOException {
    12. FileDownloadUtils.createFile(tempDir);
    13. //存在多个文件
    14. List<File> files = new ArrayList<>();
    15. String path;
    16. for (int i = 0; i < flag.length; i++) {
    17. if (flag[i].equals("worker")) {
    18. path = this.getStoreOrderExcel(flag[i], workerMaps, tempDir);
    19. } else {
    20. path = this.getStoreOrderExcel(flag[i], worksiteMaps, tempDir);
    21. }
    22. //excel添加到files中
    23. files.add(new File(path));
    24. }
    25. return files;
    26. }
    27. /**
    28. * @param flag 标识
    29. * @param maps map数组
    30. * @param tempDir 路径
    31. * @return String
    32. * @throws IOException 异常
    33. */
    34. public String getStoreOrderExcel(String flag, List<Map<String, Object>> maps, String tempDir) throws IOException {
    35. // 通过工具类创建writer,默认创建xls格式
    36. ExcelWriter writer = ExcelUtil.getWriter();
    37. if (flag.equals("worker")) {
    38. //自定义标题别名
    39. writer.addHeaderAlias("workerName", "姓名");
    40. writer.addHeaderAlias("workerIdcard", "身份证号");
    41. } else {
    42. //自定义标题别名
    43. writer.addHeaderAlias("workersiteName", "工地名称");
    44. writer.addHeaderAlias("worksiteAddress", "工地地址");
    45. }
    46. writer.write(maps, true);
    47. //生成一个excel
    48. String path = tempDir + LocalDate.now() + "_" + flag + ".xls";
    49. //本地测试下载
    50. FileOutputStream outputStream = new FileOutputStream(path);
    51. writer.flush(outputStream, true);
    52. writer.close();
    53. return path;
    54. }
    复制代码

    3、相关工具类

    1. import java.io.BufferedOutputStream;
    2. import java.io.File;
    3. import java.io.FileInputStream;
    4. import java.io.FileOutputStream;
    5. import java.util.zip.ZipEntry;
    6. import java.util.zip.ZipOutputStream;
    7. public class FileDownloadUtils {
    8. /**
    9. * 创建文件夹;
    10. *
    11. * @param path 路径
    12. */
    13. public static void createFile(String path) {
    14. File file = new File(path);
    15. //判断文件是否存在;
    16. if (!file.exists()) {
    17. //创建文件;
    18. file.mkdirs();
    19. }
    20. }
    21. /**
    22. * 删除文件夹及文件夹下所有文件
    23. *
    24. * @param dir 文件地址
    25. * @return boolean
    26. */
    27. public static boolean deleteDir(File dir) {
    28. if (dir == null || !dir.exists()) {
    29. return true;
    30. }
    31. if (dir.isDirectory()) {
    32. String[] children = dir.list();
    33. //递归删除目录中的子目录下
    34. for (String child : children) {
    35. boolean success = deleteDir(new File(dir, child));
    36. if (!success) {
    37. return false;
    38. }
    39. }
    40. }
    41. // 目录此时为空,可以删除
    42. return dir.delete();
    43. }
    44. /**
    45. * @Description 将多个文件进行压缩到指定位置
    46. * @param path 要压缩的文件路径
    47. * @param format 生成的格式(zip、rar)
    48. * @param zipPath zip的路径
    49. * @param zipName zip文件名
    50. */
    51. public static boolean generateFile(String path, String format,String zipPath,String zipName) throws Exception {
    52. File file = new File(path);
    53. // 压缩文件的路径不存在
    54. if (!file.exists()) {
    55. throw new Exception("路径 " + path + " 不存在文件,无法进行压缩...");
    56. }
    57. // 用于存放压缩文件的文件夹
    58. String generateFile = zipPath + File.separator ;
    59. File compress = new File(generateFile);
    60. // 如果文件夹不存在,进行创建
    61. if( !compress.exists() ){
    62. compress.mkdirs();
    63. }
    64. // 目的压缩文件
    65. String generateFileName = compress.getAbsolutePath() + File.separator + zipName + "." + format;
    66. // 输出流
    67. FileOutputStream outputStream = new FileOutputStream(generateFileName);
    68. // 压缩输出流
    69. ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(outputStream));
    70. //压缩
    71. generateFile(zipOutputStream,file,"");
    72. System.out.println("源文件位置:" + file.getAbsolutePath() + ",目的压缩文件生成位置:" + generateFileName);
    73. // 关闭 输出流
    74. zipOutputStream.close();
    75. return true;
    76. }
    77. /**
    78. * @param out 输出流
    79. * @param file 目标文件
    80. * @param dir 文件夹
    81. * @throws Exception
    82. */
    83. private static void generateFile(ZipOutputStream out, File file, String dir) throws Exception {
    84. // 当前的是文件夹,则进行一步处理
    85. if (file.isDirectory()) {
    86. //得到文件列表信息
    87. File[] files = file.listFiles();
    88. //将文件夹添加到下一级打包目录
    89. out.putNextEntry(new ZipEntry(dir + "/"));
    90. dir = dir.length() == 0 ? "" : dir + "/";
    91. //循环将文件夹中的文件打包
    92. for (int i = 0; i < files.length; i++) {
    93. generateFile(out, files[i], dir + files[i].getName());
    94. }
    95. } else { // 当前是文件
    96. // 输入流
    97. FileInputStream inputStream = new FileInputStream(file);
    98. // 标记要打包的条目
    99. out.putNextEntry(new ZipEntry(dir));
    100. // 进行写操作
    101. int len = 0;
    102. byte[] bytes = new byte[1024];
    103. while ((len = inputStream.read(bytes)) > 0) {
    104. out.write(bytes, 0, len);
    105. }
    106. // 关闭输入流
    107. inputStream.close();
    108. }
    109. }
    复制代码

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持晓枫资讯。


    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
    晓枫资讯-科技资讯社区-免责声明
    免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
          1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
          2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
          3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:点击这里给我发消息进行删除处理。
          4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
          5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
    http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

      离线 

    TA的专栏

    • 打卡等级:即来则安
    • 打卡总天数:23
    • 打卡月天数:1
    • 打卡总奖励:250
    • 最近打卡:2025-12-10 15:48:24
    等级头衔

    等級:晓枫资讯-列兵

    在线时间
    0 小时

    积分成就
    威望
    0
    贡献
    0
    主题
    0
    精华
    0
    金钱
    294
    积分
    56
    注册时间
    2023-1-20
    最后登录
    2025-12-10

    发表于 2024-12-9 02:38:41 | 显示全部楼层
    路过,支持一下
    http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

      离线 

    TA的专栏

    等级头衔

    等級:晓枫资讯-列兵

    在线时间
    0 小时

    积分成就
    威望
    0
    贡献
    0
    主题
    0
    精华
    0
    金钱
    11
    积分
    2
    注册时间
    2023-10-22
    最后登录
    2023-10-22

    发表于 2025-10-19 01:24:16 | 显示全部楼层
    顶顶更健康!!!
    http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~
    严禁发布广告,淫秽、色情、赌博、暴力、凶杀、恐怖、间谍及其他违反国家法律法规的内容。!晓枫资讯-社区
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    1楼
    2楼
    3楼

    手机版|晓枫资讯--科技资讯社区 本站已运行

    CopyRight © 2022-2025 晓枫资讯--科技资讯社区 ( BBS.yzwlo.com ) . All Rights Reserved .

    晓枫资讯--科技资讯社区

    本站内容由用户自主分享和转载自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

    如有侵权、违反国家法律政策行为,请联系我们,我们会第一时间及时清除和处理! 举报反馈邮箱:点击这里给我发消息

    Powered by Discuz! X3.5

    快速回复 返回顶部 返回列表