------ excel 实体 ------
@Data //get、set
@ContentRowHeight(15) // 行高
@HeadRowHeight(15) // 头行高
@ColumnWidth(20) //默认宽
public class ExportZSRelationColumn {
private static final long serialVersionUID = -6128134517321485539L;
//设置excel表头名称,index 排序
@ExcelProperty(value = "目标应用系统", index = 0)
private String toProjectName;
@ExcelProperty(value = "目标表数据库", index = 1)
private String toModelName;
@ExcelProperty(value = "目标表", index = 2)
private String toTableEnName;
@ColumnWidth(30) //某列宽
@ExcelProperty(value = "目标表字段", index = 3)
private String toColumnEnName;
@ExcelProperty(value = "源应用系统", index = 4)
private String fromProjectName;
@ExcelProperty(value = "源数据库", index = 5)
private String fromModelName;
@ExcelProperty(value = "源表名", index = 6)
private String fromTableEnName;
@ExcelProperty(value = "源字段名", index = 7)
private String fromColumnEnName;
@ExcelProperty(value = "映射方式", index = 8)
private String mapType;
@ExcelProperty(value = "映射描述", index = 9)
private String mapDesc;
}
------ 调用 ------
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
// 设置导出文件前缀
String fName = "ProcessRelation_" + sdf.format(new Date());try {
ExcelUtil.export(response, fName, "血缘", list, ExportZSRelationColumn.class);
} catch (Exception e) {
logger.error("导出失败", e.getMessage());
throw new ServiceException("导出失败");
}
------ 工具方法 ------
/**
* 导出excel
*
* @param response 响应类
* @param fileName 文件名
* @param sheetName sheet名
* @param dataList 数据列表
* @param clazz class类
* @param <T> 泛型
*/
@SneakyThrows
public static <T> void export(HttpServletResponse response, String fileName, String sheetName, List<T> dataList, Class<T> clazz) {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding(Charsets.UTF_8.name());
fileName = URLEncoder.encode(fileName, Charsets.UTF_8.name());
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), clazz).excelType(ExcelTypeEnum.XLSX).sheet(sheetName).doWrite(dataList);
}
Springboot Easyexcel
最新推荐文章于 2024-07-11 23:49:26 发布