在上一篇的《SpringBoot之Excel表动态导入数据库》中我讲解了怎么样把我们本地的Excel文件中的数据导入到服务器端的数据库里面(当然这需要Excel表中的的数据格式与数据表中的字段格式一样的前提之下才可完成),所以在这一篇文章中我要讲解的是如何动态的把数据库中的数据导出到本地的Excel文件中。好了废话咋就不多说了,直接上操作代码。(前面三步的操作和上一篇基本一样)
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
/** * 需求功能:完成服务器端把数据库中的数据读出客户端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
/** * 生育纪录表 */ 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 + "}"; } }
/** * 一个提供设置Excel表标题内容的字符串数组 * @return */ public static String[] getBirthdayRecordTitles(){ String[] titles = {"ID","头胎/其他胎","产检医院","分娩医院","计生证号码","手术日期","员工外键"}; return titles; } =============================================================================== /** * 功能分析:把对象集合转换成数组集合 * * @param list:需要操作的实体类集合 * @return * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalAccessException */ public static List
基本完成了,一些自定义的报错赋值类可查看上一篇《SpringBoot之Excel表动态导入数据库》
Lebanon Address 版权所有
Powered by WordPress