赞
踩
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.2</version>
</dependency>
CREATE TABLE `student` (
`id` bigint NOT NULL AUTO_INCREMENT,
`username` varchar(100) DEFAULT NULL,
`age` int DEFAULT NULL,
`attendtime` datetime DEFAULT NULL,
`address` varchar(256) DEFAULT NULL,
`schoolclass` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
@Data @ToString @AllArgsConstructor @NoArgsConstructor @Entity @Table(name = "student") public class Student implements Serializable { /** * 主键id也是序号,由于数据库序号设置的自增 */ @GeneratedValue(strategy= GenerationType.IDENTITY) @Id private Long id; private String username; private int age; /** * 入学时间,设置java.sql.Timestamp类型,便于后面自定义Convert类时对格式进行转换 */ private Timestamp attendtime; private String address; private int schoolclass; }
注:@GeneratedValue(strategy= GenerationType.IDENTITY) 采用数据库ID自增长的方式来自增主键字段
/** * 设置web响应输出的文件名称 * @param response web响应 * @param fileName 导出文件名称 */ private static void setResponseHeader(HttpServletResponse response, String fileName) { response.reset(); response.setContentType("application/vnd.ms-excel;charset=utf-8"); try { response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8")); } catch (UnsupportedEncodingException e) { log.error("不支持的编码", e.getLocalizedMessage()); } response.setCharacterEncoding("UTF-8"); } public static void exportStudedntExcel(HttpServletResponse response, HashMap<String,Object> datas,String fileName) throws IOException{ setResponseHeader(response, fileName); //未获取到数据时 if (datas.get("data") == null){ return; } ExcelWriter workbook= null; BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream()); try{ String template="student_fill_template.xlsx"; //日期转化格式字符串 TimestampStringConverter converter = new TimestampStringConverter(); workbook = EasyExcel.write(bos).registerConverter(converter).withTemplate(template).build(); WriteSheet sheet = EasyExcel.writerSheet("Sheet1").build(); FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build(); //填充议题数据 workbook.fill(datas.get("data"),fillConfig,sheet); //datas.remove("data"); //获取副标题及角标 HashMap<String,Object> map = new LinkedHashMap<>(); map.put("schoolclass",datas.get("schoolclass")); map.put("exporttime",datas.get("exporttime")); //填充角标及标题数据 workbook.fill(map,sheet); }catch (Throwable ex){ log.error("exportMeetExcel",ex.getMessage()); }finally { bos.flush(); if (workbook != null){ workbook.finish(); } } }
public class TimestampStringConverter implements Converter<Timestamp> { @Override public Class<?> supportJavaTypeKey() { return Timestamp.class; } @Override public CellDataTypeEnum supportExcelTypeKey() { return CellDataTypeEnum.STRING; } @Override public WriteCellData<?> convertToExcelData(Timestamp value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { WriteCellData cellData = new WriteCellData(); String cellValue; if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { cellValue = DateUtils.format(value.toLocalDateTime(), "yyyy年MM月dd日", globalConfiguration.getLocale()); } else { cellValue = DateUtils.format(value.toLocalDateTime(), contentProperty.getDateTimeFormatProperty().getFormat(), globalConfiguration.getLocale()); } cellData.setType(CellDataTypeEnum.STRING); cellData.setStringValue(cellValue); cellData.setData(cellValue); return cellData; } }
@ExcelProperty(value = "入学时间", converter = TimestampStringConverter.class)
private Timestamp attendtime;
本工程目录说明
student_fill_template.xlsx 为Excel模板,注意放置位置
该工程有两个接口
POST:http://127.0.0.1:8080/init 向student表中初始化学生信息
GET:http://127.0.0.1:8080/export/{classno} 从学生student表中导出学生信息以excel的形式保存,其中classno是班级编号,1-9,对应枚举如下:
One("高一一班",1),Two("高一二班",2),Three("高一三班",3),
Four("高二一班",4),Five("高二二班",5),Six("高二三班",6),
Seven("高三一班",5),Eight("高三二班",8),Nine("高三三班",9);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。