java的Excel导出方式总结

您所在的位置:网站首页 easypoi导入excel全是null java的Excel导出方式总结

java的Excel导出方式总结

#java的Excel导出方式总结| 来源: 网络整理| 查看: 265

一、使用hutool导出excel    1.1 hutool介绍

        hutool功能很强大,http请求到json处理、excel的导入导出、定时任务、IO、缓存、数据库操作等都提供了简单而方便的api供我们使用,好处是再也不用担心自己去整理常用的工具类了,同时也支持按需引入【但一般项目都是直接一如hutool-all 导致项目引入很多不必要的工具类】。

       从2014年首次发布第一版本到现在已经8年了,这款国产工具类确实收获了越来越多的关注,而且社区的热度是可以的,但是比起Apache或者谷歌提供的工具类,更新频率和可靠性也许稍差,但在我看来是可以考虑使用的。

   1.2 编写代码导出excel【仅表头】

 我们要的效果:

     

 所需编写的代码:

@GetMapping("/exportTemplate") public void exportTemplate(HttpServletResponse response) throws IOException { String column1Name1 = "时间戳"; String column1Name2 = "设备名称"; List headList = new ArrayList(); headList.add(column1Name1); headList.add(column1Name2); //在内存操作,写到浏览器 ExcelWriter writer= ExcelUtil.getWriter(true); // 设置表头的宽度 writer.setColumnWidth(0, 20); writer.setColumnWidth(1, 15); writer.writeHeadRow(headList).write(Collections.emptyList()); //设置content—type response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8"); //Content-disposition是MIME协议的扩展,MIME协议指示MIME用户代理如何显示附加的文件。 response.setHeader("Content-Disposition","attachment;filename="+URLEncoder.encode("数据集导入模板","UTF-8")+".xlsx"); ServletOutputStream outputStream= response.getOutputStream(); //将Writer刷新到OutPut writer.flush(outputStream,true); outputStream.close(); writer.close(); } 1.3 编写代码导出excel【含内容】

    

 我们要的效果:

     

 所需编写的代码:

@GetMapping("/exportTemplate") public void exportTemplate(HttpServletResponse response) throws IOException { String column1Name1 = "时间戳"; String column1Name2 = "设备名称"; List headList = new ArrayList(); headList.add(column1Name1); headList.add(column1Name2); //在内存操作,写到浏览器 ExcelWriter writer= ExcelUtil.getWriter(true); // 设置表头的宽度 writer.setColumnWidth(0, 20); writer.addHeaderAlias("timestamp",column1Name1); writer.setColumnWidth(1, 15); writer.addHeaderAlias("deviceName",column1Name2); // 默认的,未添加alias的属性也会写出,如果想只写出加了别名的字段,可以调用此方法排除之 writer.setOnlyAlias(true); // 表格内容【相比上一节新内容】 List excelList = new ArrayList(); CollectDataExcelVo vo1 = new CollectDataExcelVo(); vo1.setDeviceName("A类设备"); vo1.setTimestamp(DateUtil.format(new Date())); excelList.add(vo1); CollectDataExcelVo vo2 = new CollectDataExcelVo(); vo2.setDeviceName("B类设备"); vo2.setTimestamp(DateUtil.format(new Date())); excelList.add(vo2); writer.writeHeadRow(headList).write(excelList); //设置content—type response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8"); //Content-disposition是MIME协议的扩展,MIME协议指示MIME用户代理如何显示附加的文件。 response.setHeader("Content-Disposition","attachment;filename="+URLEncoder.encode("数据集导入模板","UTF-8")+".xlsx"); ServletOutputStream outputStream= response.getOutputStream(); //将Writer刷新到OutPut writer.flush(outputStream,true); outputStream.close(); writer.close(); } @Data public class CollectDataExcelVo { /** * 时间戳 */ @ApiModelProperty(value = "时间戳") private String timestamp; /** * 设备编码 */ @ApiModelProperty(value = "设备名称") private String deviceName; }

 1.4 编写代码导出excel【导出下拉列表】

 我们要的效果:

     

 所需编写的代码:

@GetMapping("/exportTemplate") public void exportTemplate(HttpServletResponse response) throws IOException { String column1Name1 = "时间戳"; String column1Name2 = "设备名称"; List headList = new ArrayList(); headList.add(column1Name1); headList.add(column1Name2); //在内存操作,写到浏览器 ExcelWriter writer= ExcelUtil.getWriter(true); // 设置表头的宽度 writer.setColumnWidth(0, 20); writer.addHeaderAlias("timestamp",column1Name1); writer.setColumnWidth(1, 15); writer.addHeaderAlias("deviceName",column1Name2); // 默认的,未添加alias的属性也会写出,如果想只写出加了别名的字段,可以调用此方法排除之 writer.setOnlyAlias(true); // 表格下拉框【相比上一节新内容】 writer.addSelect(1, 1, new String[]{"1#进线","2#进线", "3#进线"}); writer.writeHeadRow(headList).write(Collections.emptyList()); //设置content—type response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8"); //Content-disposition是MIME协议的扩展,MIME协议指示MIME用户代理如何显示附加的文件。 response.setHeader("Content-Disposition","attachment;filename="+URLEncoder.encode("数据集导入模板","UTF-8")+".xlsx"); ServletOutputStream outputStream= response.getOutputStream(); //将Writer刷新到OutPut writer.flush(outputStream,true); outputStream.close(); writer.close(); }

二、使用easyexcel导出excel 2.1 easyexcel介绍

        EasyExcel是阿里巴巴开源的一个excel处理框架,以使用简单、节省内存著称;能大大减少占用内存的主要原因是在解析Excel时没有将文件数据一次性全部加载到内存中,而是从磁盘上一行行读取数据,逐个解析。

   2.2 编写代码导出excel

 我们要的效果:

  

 所需编写的代码:

pom引入依赖:

com.alibaba easyexcel 3.0.5 org.apache.poi poi-ooxml 4.1.2

java代码:

import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.write.style.ColumnWidth; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @Data public class CollectDataExcelVo { /** * 时间戳 */ @ExcelProperty(value = "时间戳", index = 0) @ColumnWidth(value = 20) @ApiModelProperty(value = "时间戳") private String timestamp; /** * 设备名称 */ @ExcelProperty(value = "设备名称", index = 1) @ColumnWidth(value = 15) @ApiModelProperty(value = "设备名称") private String deviceName; } @GetMapping("/exportTemplate") public void exportTemplate(HttpServletResponse response) throws IOException { // 模拟数据库获取数据 List list = data(); response.setCharacterEncoding(StandardCharsets.UTF_8.name()); response.setHeader("content-Type", "application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("template"+ DateUtils.format(new Date(), "yyyy-MM-dd")+".xlsx", StandardCharsets.UTF_8.name())); EasyExcel.write(response.getOutputStream(), CollectDataExcelVo.class).sheet().doWrite(list); } private List data() { List list = ListUtils.newArrayList(); for (int i = 1; i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3