• React+后端实现导出Excle表格的功能


    最近在做一个基于React+antd前端框架的Excel导出功能,我主要在后端做了处理,这个功能完成后,便总结成一篇技术分享文章,感兴趣的小伙伴可以参考该分享来做导出excle表格功能,以下步骤同样适用于vue框架,或者JSP页面的实现。

    在做这类导出文件的功能,其实,在后端进行处理,会更容易些,虽然前端也可以进行处理,但还是建议后端来做,因为很多导出工具类基本都是很好用。

    根据以下步骤,可以很容易就实现导出Excel表格数据的功能。

    一、开发导出图标

    按钮代码:

    1 <Button type="primary" onClick={this.excelPort} >导出Button>

    二、按钮this.excelToPort的导出方法

    1. 1 excelPort = () => {
    2. 2 location.href="/test/export.do"
    3. 3 }

    三、建立Excel的Entity类(以下类可以直接复制用,无需做修改):

    需要写一个接受前端传来表格文件到后端类——Excel Bean

    1. 1 package com.test;
    2. 2
    3. 3 import lombok.Getter;
    4. 4 import lombok.Setter;
    5. 5 import org.apache.poi.xssf.usermodel.XSSFCellStyle;
    6. 6
    7. 7 @Getter
    8. 8 @Setter
    9. 9 public class ExcelBean {
    10. 10 private String headTextName; //列头(标题)名
    11. 11 private String propertyName; //对应字段名
    12. 12 private Integer cols; //合并单元格数
    13. 13 private XSSFCellStyle cellStyle;
    14. 14
    15. 15 public ExcelBean(String headTextName, String propertyName, Integer cols) {
    16. 16 super();
    17. 17 this.headTextName = headTextName;
    18. 18 this.propertyName = propertyName;
    19. 19 this.cols = cols;
    20. 20 }
    21. 21
    22. 22 }

    表格每一行的属性字段映射到数据库里的User Bean

    1. 1 package com.bqs.data.dcm.bean;
    2. 2
    3. 3 import lombok.Getter;
    4. 4 import lombok.Setter;
    5. 5
    6. 6 @Getter
    7. 7 @Setter
    8. 8 public class User {
    9. 9 private String id;
    10. 10 private String name;
    11. 11 private Integer age;
    12. 12 private String sex;
    13. 13
    14. 14 }

    四、建立Excel的工具类(无需修改可直接复制用)

    1. 1 package com.test;
    2. 2
    3. 3 import java.beans.IntrospectionException;
    4. 4 import java.lang.reflect.InvocationTargetException;
    5. 5 import java.text.SimpleDateFormat;
    6. 6 import java.util.ArrayList;
    7. 7 import java.util.Date;
    8. 8 import java.util.List;
    9. 9 import java.util.Map;
    10. 10
    11. 11 import com.test.ExcelBean;
    12. 12 import org.apache.poi.ss.util.CellRangeAddress;
    13. 13 import org.apache.poi.xssf.usermodel.XSSFCell;
    14. 14 import org.apache.poi.xssf.usermodel.XSSFCellStyle;
    15. 15 import org.apache.poi.xssf.usermodel.XSSFFont;
    16. 16 import org.apache.poi.xssf.usermodel.XSSFRow;
    17. 17 import org.apache.poi.xssf.usermodel.XSSFSheet;
    18. 18 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    19. 19
    20. 20 /**
    21. 21 * @author 朱季谦
    22. 22 * @version
    23. 23 */
    24. 24 public class ExportUtil {
    25. 25
    26. 26 /**
    27. 27 * 导出Excel表
    28. 28 * @param clazz 数据源model类型
    29. 29 * @param objs excel标题以及对应的model字段
    30. 30 * @param map 标题行数以及cell字体样式
    31. 31 * @param sheetName 工作簿名称
    32. 32 * @return
    33. 33 *
    34. 34 */
    35. 35 public static XSSFWorkbook createExcelFile(
    36. 36 Class clazz,
    37. 37 List> objs,
    38. 38 Map> map,
    39. 39 String sheetName) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
    40. 40 //创建新的工作簿
    41. 41 XSSFWorkbook workbook = new XSSFWorkbook();
    42. 42 //创建工作表
    43. 43 XSSFSheet sheet = workbook.createSheet(sheetName);
    44. 44 //设置excel的字体样式以及标题与内容的创建
    45. 45 createFont(workbook);//字体样式
    46. 46 createTableHeader(sheet,map);//创建标题
    47. 47 createTableRows(sheet,map,objs,clazz);//创建内容
    48. 48 System.out.println(workbook);
    49. 49 return workbook;
    50. 50 }
    51. 51 private static XSSFCellStyle fontStyle;
    52. 52 private static XSSFCellStyle fontStyle2;
    53. 53 private static void createFont(XSSFWorkbook workbook) {
    54. 54 //表头
    55. 55 fontStyle = workbook.createCellStyle();
    56. 56 XSSFFont font1 = workbook.createFont();
    57. 57 font1.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
    58. 58 font1.setFontName("黑体");
    59. 59 font1.setFontHeightInPoints((short) 12);//字体大小
    60. 60 fontStyle.setFont(font1);
    61. 61 fontStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);//下边框
    62. 62 fontStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
    63. 63 fontStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);//右边框
    64. 64 fontStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
    65. 65 fontStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);//居中
    66. 66 //内容
    67. 67 fontStyle2 = workbook.createCellStyle();
    68. 68 XSSFFont font2 = workbook.createFont();
    69. 69 font2.setFontName("宋体");
    70. 70 font2.setFontHeightInPoints((short)10);
    71. 71 fontStyle2.setFont(font2);
    72. 72 fontStyle2.setBorderBottom(XSSFCellStyle.BORDER_THIN);//下边框
    73. 73 fontStyle2.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
    74. 74 fontStyle2.setBorderTop(XSSFCellStyle.BORDER_THIN);//右边框
    75. 75 fontStyle2.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
    76. 76 fontStyle2.setAlignment(XSSFCellStyle.ALIGN_CENTER);//居中
    77. 77 }
    78. 78
    79. 79
    80. 80
    81. 81 /**
    82. 82 * 根据ExcelMapping 生成列头(多行列头)
    83. 83 * @param sheet 工作簿
    84. 84 * @param map 每行每个单元格对应的列头信息
    85. 85 */
    86. 86 private static void createTableHeader(
    87. 87 XSSFSheet sheet,
    88. 88 Map> map) {
    89. 89 int startIndex = 0;//cell起始位置
    90. 90 int endIndex = 0;//cell终止位置
    91. 91 for(Map.Entry> entry: map.entrySet()){
    92. 92 XSSFRow row = sheet.createRow(entry.getKey()); //创建行
    93. 93 List excels = entry.getValue();
    94. 94 for(int x=0;x
    95. 95 //合并单元格
    96. 96 if(excels.get(x).getCols()>1){
    97. 97 if(x==0){
    98. 98 endIndex += excels.get(x).getCols()-1;
    99. 99 //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
    100. 100 sheet.addMergedRegion(new CellRangeAddress(0, 0, startIndex, endIndex));
    101. 101 startIndex += excels.get(x).getCols();
    102. 102 }else{
    103. 103 endIndex += excels.get(x).getCols();
    104. 104 sheet.addMergedRegion(new CellRangeAddress(0, 0, startIndex, endIndex));
    105. 105 startIndex += excels.get(x).getCols();
    106. 106 }
    107. 107 XSSFCell cell = row.createCell(startIndex-excels.get(x).getCols());
    108. 108 //设置内容
    109. 109 cell.setCellValue(excels.get(x).getHeadTextName());
    110. 110 if(excels.get(x).getCellStyle() != null){
    111. 111 //设置格式
    112. 112 cell.setCellStyle(excels.get(x).getCellStyle());
    113. 113 }
    114. 114 cell.setCellStyle(fontStyle);
    115. 115 }else{
    116. 116 XSSFCell cell = row.createCell(x);
    117. 117 //设置内容
    118. 118 cell.setCellValue(excels.get(x).getHeadTextName());
    119. 119 if(excels.get(x).getCellStyle() != null){
    120. 120 //设置格式
    121. 121 cell.setCellStyle(excels.get(x).getCellStyle());
    122. 122 }
    123. 123 cell.setCellStyle(fontStyle);
    124. 124 }
    125. 125 }
    126. 126 }
    127. 127 }
    128. 128
    129. 129
    130. 130 /**
    131. 131 * 为excel表中循环添加数据
    132. 132 * @param sheet
    133. 133 * @param map 字段名
    134. 134 * @param objs 查询的数据
    135. 135 * @param clazz 无用
    136. 136 */
    137. 137 private static void createTableRows(
    138. 138 XSSFSheet sheet,
    139. 139 Map> map,
    140. 140 List> objs,
    141. 141 Class clazz)
    142. 142 throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    143. 143 int rowindex = map.size();
    144. 144 int maxkey = 0;
    145. 145 List ems = new ArrayList<>();
    146. 146 for(Map.Entry> entry : map.entrySet()){
    147. 147 if(entry.getKey() > maxkey){
    148. 148 maxkey = entry.getKey();
    149. 149 }
    150. 150 }
    151. 151 ems = map.get(maxkey);
    152. 152 List widths = new ArrayList(ems.size());
    153. 153 for(Map obj : objs){
    154. 154 XSSFRow row = sheet.createRow(rowindex);
    155. 155 for(int i=0;i
    156. 156 ExcelBean em = (ExcelBean)ems.get(i);
    157. 157 String propertyName = em.getPropertyName();
    158. 158 Object value = obj.get(propertyName);
    159. 159 XSSFCell cell = row.createCell(i);
    160. 160 String cellValue = "";
    161. 161 if("valid".equals(propertyName)){
    162. 162 cellValue = value.equals(1)?"启用":"禁用";
    163. 163 }else if(value==null){
    164. 164 cellValue = "";
    165. 165 }else if(value instanceof Date){
    166. 166 cellValue = new SimpleDateFormat("yyyy-MM-dd").format(value);
    167. 167 }else{
    168. 168 cellValue = value.toString();
    169. 169 }
    170. 170 cell.setCellValue(cellValue);
    171. 171 cell.setCellType(XSSFCell.CELL_TYPE_STRING);
    172. 172 cell.setCellStyle(fontStyle2);
    173. 173 sheet.autoSizeColumn(i);
    174. 174 }
    175. 175 rowindex++;
    176. 176 }
    177. 177
    178. 178 //设置列宽
    179. 179 for(int index=0;index
    180. 180 Integer width = widths.get(index);
    181. 181 width = width<2500?2500:width+300;
    182. 182 width = width>10000?10000+300:width+300;
    183. 183 sheet.setColumnWidth(index, width);
    184. 184 }
    185. 185 }
    186. 186 }

    五、导出Excel的controller类

    1. 1 /**
    2. 2 * 导出excle表格
    3. 3 */
    4. 4 @RequestMapping(value = "/export")
    5. 5 public void exportTotal( HttpServletResponse response ) throws Exception{
    6. 6 response.reset(); //清除buffer缓存
    7. 7 //Map map=new HashMap();
    8. 8 // 指定下载的文件名
    9. 9 response.setContentType("application/vnd.ms-excel;charset=UTF-8");
    10. 10 String excleName="统计表格"+".xlsx";
    11. 11 response.setHeader("Content-Disposition","attachment;filename="+new String(excleName.getBytes(),"iso-8859-1"));
    12. 12 //导出Excel对象
    13. 13 XSSFWorkbook workbook = sysExportExcelInfo.exportExcel();
    14. 14 OutputStream output;
    15. 15 try {
    16. 16 output = response.getOutputStream();
    17. 17 BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
    18. 18 bufferedOutput.flush();
    19. 19 workbook.write(bufferedOutput);
    20. 20 bufferedOutput.close();
    21. 21
    22. 22 } catch (IOException e) {
    23. 23 e.printStackTrace();
    24. 24 }
    25. 25 }

    六、导出Excel的service类

    1. 1 public XSSFWorkbook exportExcel() throws Exception{
    2. 2 //获取dao导出的list集合
    3. 3 List list=userService.exportUser();
    4. 4
    5. 5 List> listMap=ListBeanToListMap(list);
    6. 6
    7. 7 List excel = new ArrayList<>();
    8. 8 Map> map = new LinkedHashMap<>();
    9. 9 //设置标题栏
    10. 10 excel.add(new ExcelBean("序号","id",0));
    11. 11 excel.add(new ExcelBean("名字","name",0));
    12. 12 excel.add(new ExcelBean("年龄","age",0));
    13. 13
    14. 14 map.put(0,excel);
    15. 15 String sheetName = "统计表格";
    16. 16 //调用ExcelUtil方法
    17. 17 XSSFWorkbook xssfWorkbook = ExportUtil.createExcelFile(DcmDemand.class, listMap, map, sheetName);
    18. 18 System.out.println(xssfWorkbook);
    19. 19 return xssfWorkbook;
    20. 20 }

    注意:整块导出Excel代码,主要需要改动只是这一行代码:List list=userService.exportUser(),这是调用dao层获取以列表list获得数据的查询。

    下面三行代码里的“序号”,“名字”,“年龄”根据User属性来定义的,它将作为表格表头呈现在导出的表格里。这里的User表映射到数据库表t_user表,你需要导出User里哪些字段的数据,就以这样格式excel.add(new ExcelBean("序号","id",0))加到下面代码里:

    1. 1 excel.add(new ExcelBean("序号","id",0));
    2. 2 excel.add(new ExcelBean("名字","name",0));
    3. 3 excel.add(new ExcelBean("年龄","age",0));

    其中,以上代码需要把list转换成List>形式,转换方法如下,因为创建表格时需要这样List>格式类型数据:

    1. 1 public static List> ListBeanToListMap(List list) throws NoSuchMethodException,
    2. 2 SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    3. 3 List> listmap = new ArrayList>();
    4. 4
    5. 5 for (Object ob : list) {
    6. 6
    7. 7 listmap.add(beanToMap(ob));
    8. 8 }
    9. 9 return listmap;
    10. 10 }

    按照以上代码步骤,可以实现在React+antd前端实现导出这样的Excel表格功能:

    若有什么不明白的,可以评论留言,我会尽量解答。

  • 相关阅读:
    SpringBoot SpringBoot 运维实用篇 4 日志 4.4 文件记录日志
    关于Linux系统之VM安装配置(每一个步骤都超级详细的哦!)
    【前端】Vue实现个人空间
    编译安装LAMP架构搭建wordpress个人博客和discuz论坛——编译安装基于 FASTCGI 模式LAMP架构多虚拟主机WEB应用
    docker 中镜像和容器区别
    【数据库查询表结构】
    Pygame开发一个打字游戏
    分布式事务最终一致性的方案
    Oracle 19c LISTAGG 函数中distinct
    react+antd封装表格组件2.0
  • 原文地址:https://blog.csdn.net/weixin_40706420/article/details/134486968