概述
- 导出
单Sheet导出@ApiOperation("导出") @PostMapping("export") public void export(HttpServletRequest request, HttpServletResponse response) throws IOException { List<StorageDailyReportNaturalGasMagic> search = this.getList(); //写入下载命令 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 String fileName = URLEncoder.encode("日报表", "UTF-8").replaceAll("\+", "%20"); //写入文件名 response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), StorageDailyReportNaturalGasMagic.class).build()) { //构建Sheet WriteSheet writeSheet = EasyExcel.writerSheet(0, "天然气").build(); //将数据写入Sheet excelWriter.write(search, writeSheet); } }
多Sheet导出
@RequestMapping("exportExcel") @ApiOperation("数据库导出") public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException { List<NodeRedPlcDictMagic> list = this.getList(); // 根据type分组 Map<String, List<NodeRedPlcDictMagic>> groupBy = list.stream().collect(Collectors.groupingBy(NodeRedPlcDictMagic::getType)); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 String fileName = URLEncoder.encode("PLC字典", "UTF-8").replaceAll("\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); int i = 0; // 按照type分组导入到不同的Sheet中 try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), NodeRedPlcDictMagic.class).build()) { for (Map.Entry<String, List<NodeRedPlcDictMagic>> entry : groupBy.entrySet()) { //构建Sheet WriteSheet writeSheet = EasyExcel.writerSheet(i++, entry.getKey()).build(); //写入Sheet excelWriter.write(entry.getValue(), writeSheet); } } }
- 导入
最后
以上就是含蓄大米为你收集整理的阿里巴巴开源组件Easy Excel的Excel导入导出的全部内容,希望文章能够帮你解决阿里巴巴开源组件Easy Excel的Excel导入导出所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复