Hutool的Office操作

昨天听到我们头说让其他人完成一个将数据导出为excel的功能,我一开始想的是poi实现,然后问了一下发现我们公司用的是一个叫HuTool的工具类,我查看了一下官方文档,这个工具类实现的功能特别多,此次先说一下poi的操作,其他的有时间我们再聊一聊:

官方文档地址:https://www.hutool.cn/docs/#/poi/Excel生成-ExcelWriter

我做的Demo的码云地址:https://gitee.com/zym213/hutool_poi

首先引入依赖:

  1. <dependency>
  2. <groupId>cn.hutool</groupId>
  3. <artifactId>hutool-all</artifactId>
  4. <version>5.4.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.poi</groupId>
  8. <artifactId>poi</artifactId>
  9. <version>4.1.2</version>
  10. </dependency>

编写测试类:

DownloadUtil:

  1. package com.zym.testpoi.util;
  2. import cn.hutool.core.io.IoUtil;
  3. import cn.hutool.poi.excel.ExcelUtil;
  4. import cn.hutool.poi.excel.ExcelWriter;
  5. import javax.servlet.ServletOutputStream;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.UUID;
  12. public class DownloadUtil {
  13. public static void download(List<Map<String,Object>> list,HttpServletResponse response) throws IOException {
  14. String path = System.getProperty("java.io.tmpdir") + UUID.randomUUID() + ".xlsx";
  15. File file = new File(path);//在系统的临时目录生成一个excel
  16. ExcelWriter writer = ExcelUtil.getWriter();
  17. // 一次性写出内容,使用默认样式,强制输出标题
  18. writer.write(list,true);
  19. //response为HttpServletResponse对象
  20. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  21. //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
  22. response.setHeader("Content-Disposition","attachment;filename=file.xlsx");
  23. ServletOutputStream out=response.getOutputStream();
  24. writer.flush(out, true);
  25. writer.close();
  26. //此处记得关闭输出Servlet流
  27. IoUtil.close(out);
  28. // 终止后删除临时文件
  29. file.deleteOnExit();
  30. }
  31. }

User:

  1. package com.zym.testpoi.pojo;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. @Data
  6. @AllArgsConstructor
  7. @NoArgsConstructor
  8. public class User {
  9. private String username;
  10. private int age;
  11. private String hobby;
  12. }

UserController:

  1. package com.zym.testpoi.controller;
  2. import com.zym.testpoi.pojo.User;
  3. import com.zym.testpoi.service.UserService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. @Controller
  12. public class UserController {
  13. @Autowired
  14. private UserService userService;
  15. @GetMapping("/download")
  16. public void download(HttpServletResponse response) throws IOException {
  17. List<User> list = new ArrayList<>();
  18. list.add(new User("zhangsan",23,"football"));
  19. list.add(new User("lisi",24,"song"));
  20. list.add(new User("wangwu",25,"computergame"));
  21. userService.download(list,response);
  22. }
  23. }

UserServiceImpl:

  1. package com.zym.testpoi.service.impl;
  2. import com.zym.testpoi.pojo.User;
  3. import com.zym.testpoi.service.UserService;
  4. import com.zym.testpoi.util.DownloadUtil;
  5. import org.springframework.stereotype.Service;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.LinkedHashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. @Service
  13. public class UserServiceImpl implements UserService {
  14. @Override
  15. public void download(List<User> list, HttpServletResponse response) throws IOException {
  16. List<Map<String,Object>> downloadlist = new ArrayList<>();
  17. for (User user : list) {
  18. Map<String,Object> map = new LinkedHashMap<>();
  19. map.put("用户名",user.getUsername());
  20. map.put("年龄",user.getAge());
  21. map.put("爱好",user.getHobby());
  22. downloadlist.add(map);
  23. }
  24. DownloadUtil.download(downloadlist,response);
  25. }
  26. }

然后我们启动项目,调用一下download这个接口:
1.png
浏览器弹出了下载窗口,下载下来看一下是什么样子:
2.png
虽然样式丑了点,但功能实现了,接下来就是对照官方文档修改你自己想要的样式了