【Excel】Poi + Hutool Springboot 读写Excel案例
Excel处理需要的依赖:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> < dependency > < groupId >org.apache.poi</ groupId > < artifactId >poi</ artifactId > < version >5.2.2</ version > </ dependency > <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all --> < dependency > < groupId >cn.hutool</ groupId > < artifactId >hutool-all</ artifactId > < version >5.7.22</ version > </ dependency > |
数据映射的PO类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package cn.cloud9.demo.dto; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; import java.time.LocalDateTime; /** * @projectName: demo * @author: cloud9 * @date: 2022年03月30日 16:32 * @version: 1.0 */ @Data @AllArgsConstructor @NoArgsConstructor @ToString public class DictDTO { Integer id; String code; Boolean status; LocalDateTime createAt; } |
Excel表格数据
Controller类:
处理流程直接写Controller里面了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package cn.cloud9.demo.easyexcel.controller; import cn.cloud9.demo.dto.DictDTO; import cn.hutool.poi.excel.ExcelReader; import cn.hutool.poi.excel.ExcelUtil; import cn.hutool.poi.excel.ExcelWriter; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.InputStream; import java.io.OutputStream; import java.net.URLEncoder; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * @projectName: demo * @author: cloud9 * @date: 2022年03月30日 14:50 * @version: 1.0 */ @RestController @RequestMapping ( "/excel" ) public class DemoController { static Map<String, String> map = new ConcurrentHashMap<>(); static { /** * 注意别名Map设置 * 生成Excel的时候 - 属性做Key 翻译的名称标题做Value * 读取Excel的时候 - 翻译的名称标题做Key 属性做Value */ map.put( "ID" , "id" ); map.put( "代码" , "code" ); map.put( "状态" , "status" ); map.put( "创建日期" , "createAt" ); } } |
Excel文件上传进行读取
Hutool提供的ExcelReader可以简化数据装填处理
直接按照PO类和头别名映射进行数据封装:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | /** * localhost:8085/excel/upload * @param multipartFile * @return void * @author cloud9 * @createTime 2022/3/31 16:04 * */ @PostMapping ( "/upload" ) public void upload( @RequestParam (name = "excel" ) MultipartFile multipartFile) throws Exception{ // 获取文件名称 String filename = multipartFile.getOriginalFilename(); // 文件输入流 InputStream inputStream = multipartFile.getInputStream(); // 得到Hutool的Reader ExcelReader reader = ExcelUtil.getReader(inputStream); /** * 默认读取第一个Sheet * 读取为Map列表,默认第一行为标题行,Map中的key为标题,value为标题对应的单元格值。 * @param multipartFile * @return void * @author cloud9 * @createTime 2022/3/31 16:18 * */ List<Map<String, Object>> list = reader.readAll(); // PO需要设置Map来对应设置 reader.setHeaderAlias(map); // 能解析公式 List<DictDTO> list2 = reader.readAll(DictDTO. class ); System.out.println(filename); System.out.println(list); System.out.println(list2); } |
取出数据,装填到Excel文件中返回出去
这样遍历map装填不能指定标题字段的顺序
如果需要指定顺序,就需要自己一个个调用add方法手动设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | /** * localhost:8085/excel/download * @param response * @return void * @author 戴知舟 * @createTime 2022/4/1 09:21 * */ @GetMapping ( "/download" ) public void download(HttpServletResponse response) throws Exception{ // 创建模拟数据 List<DictDTO> dicts = new ArrayList<>(); dicts.add( new DictDTO( 1001 , "Code-1001" , true , LocalDateTime.now())); dicts.add( new DictDTO( 2002 , "Code-2002" , true , LocalDateTime.now())); dicts.add( new DictDTO( 3003 , "Code-3003" , true , LocalDateTime.now())); dicts.add( new DictDTO( 4004 , "Code-4004" , false , LocalDateTime.now())); dicts.add( new DictDTO( 5005 , "Code-5005" , true , LocalDateTime.now())); dicts.add( new DictDTO( 6006 , "Code-6006" , false , LocalDateTime.now())); // 获取写入器对象 ExcelWriter excelWriter = ExcelUtil.getWriter(); // 设置别名标题映射 // excelWriter.setHeaderAlias(map); × // 如果要指定字段顺序,还是一个个添加操作 map.forEach((k, v) -> excelWriter.addHeaderAlias(v, k)); // 装填数据, 并设置了标题头的信息 excelWriter.write(dicts, true ); // true 设置标题(灰色背景), false 不设置 // 获取服务的响应流,设置文件信息 String filename = "测试Excel文件-" + System.currentTimeMillis() + ".xlsx" ; response.setContentType( "application/vnd.ms-excel;charset=utf-8" ); response.setHeader( "Content-Disposition" , "attachment;filename=" + filename + ";filename*=utf-8''" + URLEncoder.encode(filename, "UTF-8" )); final OutputStream OUTPUT_STREAM = response.getOutputStream(); // 开始写入 excelWriter.flush(OUTPUT_STREAM, true ); // 关闭资源 excelWriter.close(); OUTPUT_STREAM.close(); } |
Postman文件上传测试
localhost:8085/excel/upload
调整输出代码:
1 2 | list.forEach(System.out::println); list2.forEach(System.out::println); |
控制台打印:
XLS表.xls {ID=1001, 代码=CODE-1001, 状态=true, 创建日期=2022-03-22 00:00:00} {ID=1002, 代码=CODE-1002, 状态=true, 创建日期=2022-03-23 00:00:00} {ID=1003, 代码=CODE-1003, 状态=true, 创建日期=2022-03-24 00:00:00} {ID=1004, 代码=CODE-1004, 状态=true, 创建日期=2022-03-25 00:00:00} {ID=1005, 代码=CODE-1005, 状态=true, 创建日期=2022-03-26 00:00:00} {ID=1006, 代码=CODE-1006, 状态=true, 创建日期=2022-03-27 00:00:00} {ID=1007, 代码=CODE-1007, 状态=true, 创建日期=2022-03-28 00:00:00} {ID=1008, 代码=CODE-1008, 状态=true, 创建日期=2022-03-29 00:00:00} {ID=1009, 代码=CODE-1009, 状态=true, 创建日期=2022-03-30 00:00:00} DictDTO(id=1001, code=CODE-1001, status=true, createAt=2022-03-22T00:00) DictDTO(id=1002, code=CODE-1002, status=true, createAt=2022-03-23T00:00) DictDTO(id=1003, code=CODE-1003, status=true, createAt=2022-03-24T00:00) DictDTO(id=1004, code=CODE-1004, status=true, createAt=2022-03-25T00:00) DictDTO(id=1005, code=CODE-1005, status=true, createAt=2022-03-26T00:00) DictDTO(id=1006, code=CODE-1006, status=true, createAt=2022-03-27T00:00) DictDTO(id=1007, code=CODE-1007, status=true, createAt=2022-03-28T00:00) DictDTO(id=1008, code=CODE-1008, status=true, createAt=2022-03-29T00:00) DictDTO(id=1009, code=CODE-1009, status=true, createAt=2022-03-30T00:00)
测试Excel文件下载:
http://localhost:8085/excel/download
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步