• Java中使用alibaba easyexcel导出Excel,合并单元格


    目录

     一、在pom.xml中引入 alibaba easyexcel maven 版本为2.1.7

    注意:!!!本案例不可用2.2.?以上的版本

    二、编写ExcelVo

    三、编写ExcelUtils

    四、编写ExcelService 准备数据,可以写在Controller层

    五、编写Controller层,调用接口

    六、测试导出是否成功


     一、在pom.xml中引入 alibaba easyexcel maven 版本为2.1.7

    注意:!!!本案例不可用2.2.?以上的版本

    1. com.alibaba
    2. easyexcel
    3. 2.1.7
    1. org.apache.commons
    2. commons-collections4
    3. 4.1

    二、编写ExcelVo

    1. @Data
    2. @ColumnWidth(value = 20) //列宽度 注解可以写在类上方,也可以写在字段上方
    3. @ContentRowHeight(value = 30) //列高度
    4. @HeadRowHeight(value = 35) //表头高度
    5. public class ExcelVo {
    6. @ExcelProperty(value = "表头单元格一", index = 0)
    7. private String headOne;
    8. @ExcelProperty(value = {"表头单元格二", "Come"}, index = 1)
    9. private String headTwoCome;
    10. @ExcelProperty(value = {"表头单元格二", "On"}, index = 2)
    11. private String headTwoOn;
    12. @ExcelProperty(value = {"表头单元格三", "Come"}, index = 3)
    13. private String headThreeCome;
    14. @ExcelProperty(value = {"表头单元格三", "On"}, index = 4)
    15. private String headThreeOn;
    16. @ExcelProperty(value = {"表头单元格四", "Come"}, index = 5)
    17. private String headFourCome;
    18. @ExcelProperty(value = {"表头单元格四", "On"}, index = 6)
    19. private String headFourOn;
    20. @ExcelProperty(value = {"表头单元格五", "Come"}, index = 7)
    21. private String headFiveCome;
    22. @ExcelProperty(value = {"表头单元格五", "On"}, index = 8)
    23. private String headFiveOn;
    24. @ExcelProperty(value = {"表头单元格六", "Come"}, index = 9)
    25. private String headSixCome;
    26. @ExcelProperty(value = {"表头单元格六", "On"}, index = 10)
    27. private String headSixOn;
    28. @ColumnWidth(value = 30)
    29. @ExcelProperty(value = "表头单元格七", index = 11)
    30. private String headSeven;
    31. }

    三、编写ExcelUtils

    1. import com.alibaba.excel.EasyExcel;
    2. import com.alibaba.excel.write.builder.ExcelWriterBuilder;
    3. import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy;
    4. import com.alibaba.excel.write.metadata.style.WriteCellStyle;
    5. import com.alibaba.excel.write.metadata.style.WriteFont;
    6. import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
    7. import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy;
    8. import org.apache.poi.ss.usermodel.BorderStyle;
    9. import org.apache.poi.ss.usermodel.HorizontalAlignment;
    10. import org.apache.poi.ss.usermodel.IndexedColors;
    11. import org.apache.poi.ss.usermodel.VerticalAlignment;
    12. import springboot.redis.demo.model.RetailTargetExcelVo;
    13. import javax.servlet.http.HttpServletResponse;
    14. import java.net.URLEncoder;
    15. import java.util.List;
    16. public class ExcelUtils {
    17. public static void exportExcel(HttpServletResponse response, List data)throws Exception{
    18. //1、设置数据表格的样式
    19. // ---------- 头部样式 ----------
    20. WriteCellStyle headStyle = new WriteCellStyle();
    21. // 字体样式
    22. WriteFont headFont = new WriteFont();
    23. headFont.setFontHeightInPoints((short) 11);
    24. headFont.setFontName("宋体");
    25. headFont.setColor(IndexedColors.BLACK.index);
    26. headStyle.setWriteFont(headFont);
    27. WriteCellStyle contentStyle = new WriteCellStyle();
    28. //垂直居中
    29. contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    30. //水平居中
    31. contentStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
    32. // 设置边框
    33. // bodyStyle.setBorderTop(BorderStyle.DOUBLE);
    34. contentStyle.setBorderLeft(BorderStyle.THIN);
    35. contentStyle.setBorderRight(BorderStyle.THIN);
    36. contentStyle.setBorderBottom(BorderStyle.THIN);
    37. WriteFont writeFont = new WriteFont();
    38. //加粗
    39. //字体大小为11
    40. writeFont.setFontHeightInPoints((short) 11);
    41. writeFont.setFontName("宋体");
    42. writeFont.setColor(IndexedColors.BLACK.index);
    43. contentStyle.setWriteFont(writeFont);
    44. // 创建单元格策略1 参数1为头样式【不需要头部,设置为null】,参数2位表格内容样式
    45. HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headStyle, contentStyle);
    46. // 创建策略2
    47. // HorizontalCellStyleStrategy dataTableStrategy = new HorizontalCellStyleStrategy(headStyle,bodyStyle);
    48. // 设置数据表格的行高 null表示使用原来的行高
    49. // SimpleRowHeightStyleStrategy rowHeightStrategy3 = new SimpleRowHeightStyleStrategy( null, (short) 18);
    50. //循环合并策略
    51. // LoopMergeStrategy loopMergeStrategy = new LoopMergeStrategy(2, 0);
    52. //一次绝对合并策略
    53. OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy1 = new OnceAbsoluteMergeStrategy(0, 1, 1, 2); //0,1表示第1行到第2行 1,2表示第2列到第3列
    54. OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy2 = new OnceAbsoluteMergeStrategy(0, 1, 3, 4);
    55. OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy3 = new OnceAbsoluteMergeStrategy(0, 1, 5, 6);
    56. OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy4 = new OnceAbsoluteMergeStrategy(0, 1, 7, 8);
    57. OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy5 = new OnceAbsoluteMergeStrategy(0, 1, 9, 10);
    58. // response.setContentType("application/vnd.ms-excel");
    59. response.setCharacterEncoding("utf-8");
    60. String fileName = URLEncoder.encode("文件名", "UTF-8");
    61. response.setHeader("content-type", "application/octet-stream");
    62. response.setContentType("application/octet-stream");
    63. response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
    64. ExcelWriterBuilder excelWriterBuilder = EasyExcel.write(response.getOutputStream(), ExcelVo.class)
    65. // .registerWriteHandler(loopMergeStrategy) // 循环合并策略
    66. .registerWriteHandler(onceAbsoluteMergeStrategy1)
    67. .registerWriteHandler(onceAbsoluteMergeStrategy2)
    68. .registerWriteHandler(onceAbsoluteMergeStrategy3)
    69. .registerWriteHandler(onceAbsoluteMergeStrategy4)
    70. .registerWriteHandler(onceAbsoluteMergeStrategy5)
    71. // .registerWriteHandler(dataTableStrategy) //策略2
    72. .registerWriteHandler(horizontalCellStyleStrategy); //策略1
    73. excelWriterBuilder.sheet("sheet名称").doWrite(data);
    74. }
    75. }

    四、编写ExcelService 准备数据,可以写在Controller层

    1. public class ExcelService {
    2. public static List getExcelExportData() {
    3. ExcelVo excelVo = new ExcelVo();
    4. List list = new ArrayList<>();
    5. for (int i = 0; i < 3; i++) {
    6. excelVo.setHeadOne("qqqq");
    7. excelVo.setHeadTwoCome("wwww");
    8. excelVo.setHeadTwoOn("eee");
    9. excelVo.setHeadThreeCome("rrr");
    10. excelVo.setHeadThreeOn("bgbd");
    11. excelVo.setHeadFourCome("qoqo");
    12. excelVo.setHeadFourOn("规格");
    13. excelVo.setHeadFiveCome("项链");
    14. excelVo.setHeadFiveOn("等等");
    15. excelVo.setHeadSixCome("测试");
    16. excelVo.setHeadSixOn("测试");
    17. excelVo.setHeadSeven("测试");
    18. list.add(excelVo);
    19. }
    20. return list;
    21. }
    22. }

    五、编写Controller层,调用接口

    1. @RestController
    2. public class ExcelController {
    3. //方式一:准备测试数据 ExcelService.getExcelExportData()
    4. @GetMapping("/export")
    5. public void export(HttpServletResponse response) throws Exception {
    6. List data = ExcelService.getExcelExportData();
    7. ExcelUtils.exportExcel(response,data);
    8. }
    9. //方式二:通过前端传值测试,前端传的excelVo 字段和ExcelVo类里面的字段对应
    10. @GetMapping("/exportTwo")
    11. public void aliExportDetail(@RequestBody List excelVo, HttpServletResponse response) throws Exception {
    12. ExcelUtils.exportExcel(response,excelVo);
    13. }
    14. }

    六、测试导出是否成功

    在浏览器地址栏中访问:http://localhost:端口号/export

     这样就可以弹出来要保存文件的地址,点击保存,就保存成功了

    如果不知道端口号,

    配置一个application.yml文件 在文件中简单配置server port  就可以,如果有用到redis和数据库,就要配置redis和数据库

    1. # 端口号配置
    2. server:
    3. port: 8081
    4. # spring相关 配置
    5. spring:
    6. redis: # 配置redis
    7. database: 0
    8. host: localhost
    9. port: 6379
    10. datasource:# 配置数据库
    11. name:
    12. url: jdbc:mysql://ip地址:3306/test?useSSL=false
    13. username:
    14. password:
    15. driver-class-name: com.mysql.jdbc.Driver #mysql 驱动程序
    16. profiles:
    17. active: def,dev,master
    18. # 配置mybatis
    19. mybatis:
    20. mapper-locations: classpath:*mapper/*.xml
    21. type-aliases-package: com.oda.mall.entity
  • 相关阅读:
    021 gtsam/examples/Pose3SLAMExample_changeKeys.cpp
    【推荐系统中的Hash 2】局部敏感哈希(利用冲突)
    1、项目准备与新建
    c++千万数据级别正确使用无锁队列,避免内存撕碎(二)
    实训十八:RIP2邻居认证
    由于找不到vcruntime140_1.dll怎么修复,详细修复步骤分享
    设计模式之MVC模式
    SOLID之DIP-依赖反转原则
    mac拷贝文件到u盘,mac拷贝文件到u盘很慢
    离散数学复习:命题逻辑
  • 原文地址:https://blog.csdn.net/SUMMERENT/article/details/126478625