前后端分离,后端虽然用了Swagger但有时候还是需要手动维护文档的,Java实体字段很多的时候,维护起来还是很费时间。本文介绍如何通过Java反射和EasyExcel把实体信息导出为Excel表格,表格可用于维护接口文档。
大致思路
- 通过Java反射得到字段名、字段类型和注释信息。
- 通过EasyExcel导出。
完整源码
pom.xml引入的依赖:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.6</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> <scope>provided</scope> </dependency> </dependencies>
|
ApiModelProperty:
1 2 3 4 5
| @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface ApiModelProperty { String value(); }
|
Awards是需要导出到Excel的类,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| @Data public class Awards {
@ApiModelProperty(value="id主键") private Long id;
@ApiModelProperty(value="awardType获奖类型:1双奖 2科技成果转化奖 3双创奖 4专利奖 5自主开发大赛奖 6在岗技术革新奖 7创客马拉松 8外部获奖") private Integer awardType;
@ApiModelProperty(value="topClass奖项等级-一级分类") private String topClass;
@ApiModelProperty(value="secondClass奖项等级-二级分类") private String secondClass;
@ApiModelProperty(value="awardYear获奖年份:专利奖可为字符") private String awardYear;
@ApiModelProperty(value="awardProduct获奖/成果/专利产品名") private String awardProduct;
@ApiModelProperty(value="briefIntroduction成果简介") private String briefIntroduction;
@ApiModelProperty(value="awardMoney成果奖金") private BigDecimal awardMoney;
@ApiModelProperty(value="innovationPoints亮点及创新点") private String innovationPoints;
@ApiModelProperty(value="applicationEffect应用成效及前景") private String applicationEffect;
@ApiModelProperty(value="displayDiagram项目展示图,以“,”拼接附件表主键") private String displayDiagram;
@ApiModelProperty(value="projectData项目资料,以“,”拼接附件表主键") private String projectData;
@ApiModelProperty(value="patentNo专利号") private String patentNo;
@ApiModelProperty(value="patentType专利类型:1发明型 2实用新型") private Integer patentType;
@ApiModelProperty(value="applyDate申请日") private Date applyDate;
@ApiModelProperty(value="organizer主办/归属单位") private String organizer;
@ApiModelProperty(value="createUser创建人") private Integer createUser;
@ApiModelProperty(value="createTime创建时间") private Date createTime;
@ApiModelProperty(value="lastUpdateUser最后修改人") private Integer lastUpdateUser;
@ApiModelProperty(value="updateTime修改时间") private Date updateTime; }
|
ExportData类对应Excel的列名,代码如下:
1 2 3 4 5 6 7 8 9 10
| @Data public class ExportData {
@ExcelProperty(value="字段名",index = 0) private String name; @ExcelProperty(value="字段类型",index = 1) private String type; @ExcelProperty(value="字段说明",index = 2) private String description; }
|
ExportTest是程序测试入口,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public class ExportTest { public static void main(String[] args) { List<ExportData> exportDataList = getExportData(Awards.class); EasyExcel.write("exportJavaBean.xlsx", ExportData.class).sheet("JavaBean导出Excel表格").doWrite(exportDataList); }
private static List<ExportData> getExportData(Class cls){ Field[] fields = cls.getDeclaredFields(); List<ExportData> exportDataList = new ArrayList<>(); for(Field field: fields){ String typeName = field.getGenericType().getTypeName(); String tyNameNew = typeName.substring(typeName.lastIndexOf(".")+1); ApiModelProperty apiModelProperty = field.getAnnotation(ApiModelProperty.class); ExportData data = new ExportData(); data.setType(tyNameNew); data.setName(field.getName()); data.setDescription(apiModelProperty.value()); exportDataList.add(data); } return exportDataList; } }
|
最终导出的Excel如下: