SpringBoot之把数据库导出到Excel表

SpringBoot之把数据库导出到Excel表

在上一篇的《SpringBoot之Excel表动态导入数据库》中我讲解了怎么样把我们本地的Excel文件中的数据导入到服务器端的数据库里面(当然这需要Excel表中的的数据格式与数据表中的字段格式一样的前提之下才可完成),所以在这一篇文章中我要讲解的是如何动态的把数据库中的数据导出到本地的Excel文件中。好了废话咋就不多说了,直接上操作代码。(前面三步的操作和上一篇基本一样)

  1. 用maven导入一些jar包
  	org.apache.poi 	poi 	3.11   	org.apache.poi 	poi-ooxml 	3.9   	commons-fileupload 	commons-fileupload 	1.3.1   	commons-io 	commons-io 	2.4    	net.sf.json-lib 	json-lib 	2.4 	jdk15   	org.codehaus.jackson 	jackson-mapper-asl 	1.9.11   	org.codehaus.jackson 	jackson-core-asl 	1.9.11    	com.alibaba 	fastjson 	1.2.7    	com.fasterxml.jackson.core 	jackson-core 	2.8.7   	com.fasterxml.jackson.core 	jackson-annotations 	2.8.0   	com.fasterxml.jackson.core 	jackson-databind 	2.8.7  
  1. 前端代码(采用的是layui 框架,当然我是修改框架里面的源码的)
 
  1. JavaScript前端页面逻辑操作
  
  1. Controller控制层
/**      * 需求功能:完成服务器端把数据库中的数据读出客户端Excel文件的功能      *      * @param response //把生成的Excel表响应到客户端      * @throws NoSuchMethodException //报错      * @throws IllegalAccessException   //报错      * @throws InvocationTargetException    //报错      * @throws InstantiationException   //报错      */     @GetMapping("/exportExcel")     public void exportExcel(HttpServletResponse response) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {         //先把数据库中的数据查询出来         List list1 = birthdayRecordService.selectList(null);         //一个设置Excel表标题信息的工具类,获取Excel表标题的字符串数组         String[] strings = ExcelTitlesHelperUtils.getBirthdayRecordTitles();         //一个能把对象集合转换成字符串数组集合的工具类,参数为对象集合,返回字符串数组集合         List list = ExcelValuesHelperUtils.exportExcel(list1);         try {             //一个能创建Excel表并完成发送客户端的工具类,第一个参数为字符串数组集合(Excel表内容),第二个参数为字符串数组(Excel表标题),第三个参数为响应器             ExportExcelUtils.createExcelUtils(list, strings, response);         } catch (Exception e){             //导表发生异常的时候             throw new MyException(ResultEnum.EXPORT_EXCEL_ERROR.getCode(),ResultEnum.EXPORT_EXCEL_ERROR.getMsg());         }      } 
  1. 实体类
/**  * 生育纪录表  */ public class BirthdayRecord implements Serializable {      private static final long serialVersionUID = 4849616019539107195L;      /**      * 自增主键      */     @TableId(value = "bir_id", type = IdType.AUTO)     private Integer birId;     /**      * 头胎还是其他胎      */     private String birType;     /**      * 产检医院      */     private String birProdInspHos;     /**      * 分娩医院      */     private String birMaterHos;     /**      * 计生证号码      */     private String birNumber;     /**      * 手术日期      */     @DateTimeFormat(pattern = "yyyy-MM-dd")     private Date birOperationTime;     /**      * 员工外键      */     private Integer empId;      public BirthdayRecord() {     }      public BirthdayRecord(Integer birId, String birType, String birProdInspHos, String birMaterHos, String birNumber, Date birOperationTime, Integer empId) {         this.birId = birId;         this.birType = birType;         this.birProdInspHos = birProdInspHos;         this.birMaterHos = birMaterHos;         this.birNumber = birNumber;         this.birOperationTime = birOperationTime;         this.empId = empId;     }      public Integer getBirId() {         return birId;     }      public void setBirId(Integer birId) {         this.birId = birId;     }      public String getBirType() {         return birType;     }      public void setBirType(String birType) {         this.birType = birType;     }      public String getBirProdInspHos() {         return birProdInspHos;     }      public void setBirProdInspHos(String birProdInspHos) {         this.birProdInspHos = birProdInspHos;     }      public String getBirMaterHos() {         return birMaterHos;     }      public void setBirMaterHos(String birMaterHos) {         this.birMaterHos = birMaterHos;     }      public String getBirNumber() {         return birNumber;     }      public void setBirNumber(String birNumber) {         this.birNumber = birNumber;     }      public Date getBirOperationTime() {         return birOperationTime;     }      public void setBirOperationTime(Date birOperationTime) {         this.birOperationTime = birOperationTime;     }      public Integer getEmpId() {         return empId;     }      public void setEmpId(Integer empId) {         this.empId = empId;     }      @Override     public String toString() {         return "BirthdayRecord{" +         ", birId=" + birId +         ", birType=" + birType +         ", birProdInspHos=" + birProdInspHos +         ", birMaterHos=" + birMaterHos +         ", birNumber=" + birNumber +         ", birOperationTime=" + birOperationTime +         ", empId=" + empId +         "}";     } } 
  1. 一些辅助的工具方法
/**      * 一个提供设置Excel表标题内容的字符串数组      * @return      */     public static String[] getBirthdayRecordTitles(){         String[] titles = {"ID","头胎/其他胎","产检医院","分娩医院","计生证号码","手术日期","员工外键"};         return titles;     }  ===============================================================================  /**      * 功能分析:把对象集合转换成数组集合      *      * @param list:需要操作的实体类集合      * @return      * @throws NoSuchMethodException      * @throws InvocationTargetException      * @throws IllegalAccessException      */     public static List exportExcel(List list) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {         //设置用到的变量参数         String name = null, type = null, m_name = null;         Object value = null;         //创建一个二维字符串数组存放数据         List list1 = new ArrayList<>();         //获取实体类方法操作对象         Method m = null;         //判断要提取实体类属性数据的集合是否为空         if (list != null){             //遍历集合中的实体类             for (int y=0; y values, String[] titles, HttpServletResponse httpServletResponse) throws IllegalAccessException, InstantiationException {         // 第一步,创建一个webbook,对应一个Excel文件         HSSFWorkbook wb = new HSSFWorkbook();         // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet         HSSFSheet sheet = wb.createSheet("会议签到表");         // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short         HSSFRow row = sheet.createRow((int) 0);         // 第四步,创建单元格,并设置值表头 设置表头居中         HSSFCellStyle style = wb.createCellStyle();         style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式           //第五步,创建标题         for (int i =0 ; i < titles.length ; i++){             HSSFCell cell = row.createCell((short) i);             cell.setCellValue(titles[i]);         }            //第六步,写入实体数据,遍历创建内容         for(int i=0;i

基本完成了,一些自定义的报错赋值类可查看上一篇《SpringBoot之Excel表动态导入数据库》