公司用到了easypoi导出,发现网上关于easypoi合并的例子真的很少,没找到,于是自己研究了下,项目是Spring Boot

    1. <dependency>
    2. <groupId>cn.afterturn</groupId>
    3. <artifactId>easypoi-base</artifactId>
    4. <version>3.0.1</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>cn.afterturn</groupId>
    8. <artifactId>easypoi-web</artifactId>
    9. <version>3.0.1</version>
    10. </dependency>
    11. <dependency>
    12. <groupId>cn.afterturn</groupId>
    13. <artifactId>easypoi-annotation</artifactId>
    14. <version>3.0.1</version>
    15. </dependency>

    合并单元格主要有两个注解
    1、@Excel这个是必不可少的,主要是要有一个参数设置needMerge = true
    源码这样解释的
    image.png
    2、@ExcelCollection此注解用于集合这种的多行
    image.png
    案例: CustomerCluesExportVo
    image.png
    案例:DealInfoExportVo
    image.png
    使用方法

    1. 一方的@Excel设置needMerge = true,框架默认是false,多方List(其他集合)使用@ExcelCollection,@ExcelCollection默认ArrayList


    成品
    image.png
    如有需要,以下两个工具类可以直接使用 EasyPoiUtils

    1. package com.shinedata.util;
    2. import cn.afterturn.easypoi.excel.ExcelExportUtil;
    3. import cn.afterturn.easypoi.excel.ExcelImportUtil;
    4. import cn.afterturn.easypoi.excel.entity.ExportParams;
    5. import cn.afterturn.easypoi.excel.entity.ImportParams;
    6. import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
    7. import org.apache.commons.lang3.StringUtils;
    8. import org.apache.poi.ss.usermodel.Workbook;
    9. import org.springframework.web.multipart.MultipartFile;
    10. import javax.servlet.http.HttpServletResponse;
    11. import java.io.File;
    12. import java.io.IOException;
    13. import java.io.InputStream;
    14. import java.net.URLEncoder;
    15. import java.util.List;
    16. import java.util.Map;
    17. import java.util.NoSuchElementException;
    18. /**
    19. * @Description:导出导入工具类
    20. * @date 2019年8月21日 下午6:13:54
    21. */
    22. public class EasyPoiUtils {
    23. public static void sellerExportExcel( List<?> list, String title, String sheetName,
    24. Class<?> pojoClass, String fileName,
    25. HttpServletResponse response) {
    26. defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
    27. }
    28. /**
    29. * 功能描述:复杂导出Excel,包括文件名以及表名。创建表头
    30. *
    31. * @param list 导出的实体类
    32. * @param title 表头名称
    33. * @param sheetName sheet表名
    34. * @param pojoClass 映射的实体类
    35. * @param isCreateHeader 是否创建表头
    36. * @param fileName
    37. * @param response
    38. * @return
    39. */
    40. public static void exportExcel( List<?> list, String title, String sheetName, Class<?> pojoClass,
    41. String fileName, boolean isCreateHeader,
    42. HttpServletResponse response) {
    43. ExportParams exportParams = new ExportParams(title, sheetName);
    44. exportParams.setCreateHeadRows(isCreateHeader);
    45. exportParams.setStyle(ExcelStyleUtils.class);
    46. defaultExport(list, pojoClass, fileName, response, exportParams);
    47. }
    48. /**
    49. * 功能描述:复杂导出Excel,包括文件名以及表名,不创建表头
    50. *
    51. * @param list 导出的实体类
    52. * @param title 表头名称
    53. * @param sheetName sheet表名
    54. * @param pojoClass 映射的实体类
    55. * @param fileName
    56. * @param response
    57. * @return
    58. */
    59. public static void exportExcel( List<?> list, String title, String sheetName, Class<?> pojoClass,
    60. String fileName, HttpServletResponse response,
    61. Integer userMasterId) {
    62. defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
    63. }
    64. /**
    65. * 功能描述:Map 集合导出
    66. *
    67. * @param list 实体集合
    68. * @param fileName 导出的文件名称
    69. * @param response
    70. * @return
    71. */
    72. public static void exportExcel( List<Map<String, Object>> list, String fileName,
    73. HttpServletResponse response) {
    74. defaultExport(list, fileName, response);
    75. }
    76. /**
    77. * 功能描述:默认导出方法
    78. *
    79. * @param list 导出的实体集合
    80. * @param fileName 导出的文件名
    81. * @param pojoClass pojo实体
    82. * @param exportParams ExportParams封装实体
    83. * @param response
    84. * @return
    85. */
    86. private static void defaultExport( List<?> list, Class<?> pojoClass, String fileName,
    87. HttpServletResponse response, ExportParams exportParams) {
    88. Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
    89. if (workbook != null) {
    90. downLoadExcel(fileName, response, workbook);
    91. }
    92. }
    93. /**
    94. * 功能描述:Excel导出
    95. *
    96. * @param fileName 文件名称
    97. * @param response
    98. * @param workbook Excel对象
    99. * @return
    100. */
    101. private static void downLoadExcel( String fileName, HttpServletResponse response,
    102. Workbook workbook) {
    103. try {
    104. response.setCharacterEncoding("UTF-8");
    105. response.setHeader("content-Type", "multipart/form-data");
    106. response.setHeader("Content-Disposition",
    107. "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
    108. workbook.write(response.getOutputStream());
    109. } catch (IOException e) {
    110. throw new RuntimeException(e);
    111. }
    112. }
    113. /**
    114. * 功能描述:默认导出方法
    115. *
    116. * @param list 导出的实体集合
    117. * @param fileName 导出的文件名
    118. * @param response
    119. * @return
    120. */
    121. private static void defaultExport( List<Map<String, Object>> list, String fileName,
    122. HttpServletResponse response) {
    123. Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
    124. if (workbook != null)
    125. ;
    126. downLoadExcel(fileName, response, workbook);
    127. }
    128. /**
    129. * 功能描述:根据文件路径来导入Excel
    130. *
    131. * @param filePath 文件路径
    132. * @param titleRows 表标题的行数
    133. * @param headerRows 表头行数
    134. * @param pojoClass Excel实体类
    135. * @return
    136. */
    137. public static <T> List<T> importExcel( String filePath, Integer titleRows, Integer headerRows,
    138. Class<T> pojoClass) {
    139. //判断文件是否存在
    140. if (StringUtils.isBlank(filePath)) {
    141. return null;
    142. }
    143. ImportParams params = new ImportParams();
    144. params.setTitleRows(titleRows);
    145. params.setHeadRows(headerRows);
    146. List<T> list = null;
    147. File file = new File(filePath);
    148. try {
    149. list = ExcelImportUtil.importExcel(file, pojoClass, params);
    150. } catch (NoSuchElementException e) {
    151. throw new RuntimeException("模板不能为空");
    152. } catch (Exception e) {
    153. e.printStackTrace();
    154. }
    155. return list;
    156. }
    157. /**
    158. * 功能描述:根据文件流来导入Excel
    159. *
    160. * @param filePath 文件路径
    161. * @param titleRows 表标题的行数
    162. * @param headerRows 表头行数
    163. * @param pojoClass Excel实体类
    164. * @return
    165. */
    166. public static <T> List<T> importExcel( InputStream is, Integer titleRows, Integer headerRows,
    167. Class<T> pojoClass) {
    168. //判断文件是否存在
    169. if (is == null) {
    170. return null;
    171. }
    172. ImportParams params = new ImportParams();
    173. params.setTitleRows(titleRows);
    174. params.setHeadRows(headerRows);
    175. List<T> list = null;
    176. try {
    177. list = ExcelImportUtil.importExcel(is, pojoClass, params);
    178. } catch (NoSuchElementException e) {
    179. throw new RuntimeException("模板不能为空");
    180. } catch (Exception e) {
    181. e.printStackTrace();
    182. }
    183. return list;
    184. }
    185. /**
    186. * 功能描述:根据接收的Excel文件来导入Excel,并封装成实体类
    187. *
    188. * @param file 上传的文件
    189. * @param titleRows 表标题的行数
    190. * @param headerRows 表头行数
    191. * @param pojoClass Excel实体类
    192. * @return
    193. */
    194. public static <T> List<T> importExcel( MultipartFile file, Integer titleRows, Integer headerRows,
    195. Class<T> pojoClass) {
    196. if (file == null) {
    197. return null;
    198. }
    199. ImportParams params = new ImportParams();
    200. params.setTitleRows(titleRows);
    201. params.setHeadRows(headerRows);
    202. List<T> list = null;
    203. try {
    204. list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
    205. } catch (NoSuchElementException e) {
    206. throw new RuntimeException("excel文件不能为空");
    207. } catch (Exception e) {
    208. throw new RuntimeException(e.getMessage());
    209. }
    210. return list;
    211. }
    212. }

    ExcelStyleUtils

    1. package com.shinedata.util;
    2. import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
    3. import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams;
    4. import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;
    5. import org.apache.poi.ss.usermodel.*;
    6. /**
    7. * @ClassName ExcelStyleUtils
    8. * @Date 2020/7/14 15:23
    9. */
    10. public class ExcelStyleUtils implements IExcelExportStyler {
    11. private static final short STRING_FORMAT = (short) BuiltinFormats.getBuiltinFormat("TEXT");
    12. private static final short FONT_SIZE_TEN = 10;
    13. private static final short FONT_SIZE_ELEVEN = 11;
    14. private static final short FONT_SIZE_TWELVE = 12;
    15. /**
    16. * 大标题样式
    17. */
    18. private CellStyle headerStyle;
    19. /**
    20. * 每列标题样式
    21. */
    22. private CellStyle titleStyle;
    23. /**
    24. * 数据行样式
    25. */
    26. private CellStyle styles;
    27. public ExcelStyleUtils(Workbook workbook) {
    28. this.init(workbook);
    29. }
    30. /**
    31. * 初始化样式
    32. *
    33. * @param workbook
    34. */
    35. private void init(Workbook workbook) {
    36. this.headerStyle = initHeaderStyle(workbook);
    37. this.titleStyle = initTitleStyle(workbook);
    38. this.styles = initStyles(workbook);
    39. }
    40. /**
    41. * 大标题样式
    42. *
    43. * @param color
    44. * @return
    45. */
    46. @Override
    47. public CellStyle getHeaderStyle(short color) {
    48. return headerStyle;
    49. }
    50. /**
    51. * 每列标题样式
    52. *
    53. * @param color
    54. * @return
    55. */
    56. @Override
    57. public CellStyle getTitleStyle(short color) {
    58. return titleStyle;
    59. }
    60. /**
    61. * 数据行样式
    62. *
    63. * @param parity 可以用来表示奇偶行
    64. * @param entity 数据内容
    65. * @return 样式
    66. */
    67. @Override
    68. public CellStyle getStyles(boolean parity, ExcelExportEntity entity) {
    69. return styles;
    70. }
    71. /**
    72. * 获取样式方法
    73. *
    74. * @param dataRow 数据行
    75. * @param obj 对象
    76. * @param data 数据
    77. */
    78. public CellStyle getStyles(Cell cell, int dataRow, ExcelExportEntity entity, Object obj, Object data) {
    79. return getStyles(true, entity);
    80. }
    81. /**
    82. * 模板使用的样式设置
    83. */
    84. @Override
    85. public CellStyle getTemplateStyles(boolean isSingle, ExcelForEachParams excelForEachParams) {
    86. return null;
    87. }
    88. /**
    89. * 初始化--大标题样式
    90. *
    91. * @param workbook
    92. * @return
    93. */
    94. private CellStyle initHeaderStyle(Workbook workbook) {
    95. CellStyle style = getBaseCellStyle(workbook);
    96. style.setFont(getFont(workbook, FONT_SIZE_TWELVE, true));
    97. return style;
    98. }
    99. /**
    100. * 初始化--每列标题样式
    101. *
    102. * @param workbook
    103. * @return
    104. */
    105. private CellStyle initTitleStyle(Workbook workbook) {
    106. CellStyle style = getBaseCellStyle(workbook);
    107. style.setFont(getFont(workbook, FONT_SIZE_ELEVEN, false));
    108. //背景色
    109. style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    110. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    111. return style;
    112. }
    113. /**
    114. * 初始化--数据行样式
    115. *
    116. * @param workbook
    117. * @return
    118. */
    119. private CellStyle initStyles(Workbook workbook) {
    120. CellStyle style = getBaseCellStyle(workbook);
    121. style.setFont(getFont(workbook, FONT_SIZE_TEN, false));
    122. style.setDataFormat(STRING_FORMAT);
    123. return style;
    124. }
    125. /**
    126. * 基础样式
    127. *
    128. * @return
    129. */
    130. private CellStyle getBaseCellStyle(Workbook workbook) {
    131. CellStyle style = workbook.createCellStyle();
    132. //下边框
    133. style.setBorderBottom(BorderStyle.THIN);
    134. //左边框
    135. style.setBorderLeft(BorderStyle.THIN);
    136. //上边框
    137. style.setBorderTop(BorderStyle.THIN);
    138. //右边框
    139. style.setBorderRight(BorderStyle.THIN);
    140. //水平居中
    141. style.setAlignment(HorizontalAlignment.CENTER);
    142. //上下居中
    143. style.setVerticalAlignment(VerticalAlignment.CENTER);
    144. //设置自动换行
    145. style.setWrapText(true);
    146. return style;
    147. }
    148. /**
    149. * 字体样式
    150. *
    151. * @param size 字体大小
    152. * @param isBold 是否加粗
    153. * @return
    154. */
    155. private Font getFont(Workbook workbook, short size, boolean isBold) {
    156. Font font = workbook.createFont();
    157. //字体样式
    158. font.setFontName("宋体");
    159. //是否加粗
    160. font.setBold(isBold);
    161. //字体大小
    162. font.setFontHeightInPoints(size);
    163. return font;
    164. }
    165. }

    调用

    1. EasyPoiUtils.exportExcel(exportVos, title, fileName, CustomerCluesExportVo.class, fileName, true, response);

    合并单元格(groupName)
    image.png
    image.png
    image.png