当前位置:网站首页>使用反射实现动态修改@Excel的注解属性
使用反射实现动态修改@Excel的注解属性
2022-07-27 05:51:00 【子非我鱼】
业务场景:我们使用poi实现数据导出时,通常是根据@Excel(name=“xxx”)来确定列名。通常情况下这个是不会发生变动的。但这里就说少数情况,在我们需要这里根据某些情况来进行改变的时候,我们就需要用到反射。
AirQualityRankingResp.java
@Data
public class AirQualityRankingResp {
/** 站点名称 */
@Excel(name = "站点名称")
private String site;
private String siteNumber;
@Excel(name = "分析时间")
private String dateTime;
/** 污染因子 */
private String pollutionFactor;
@Excel(name = "AQI")
private Double value;
/** 排名 */
@Excel(name = "排名")
private Integer ranking;
}
正常使用@Excel导出数据如下

动态修改@Excel的属性,代码实现
/** * 通过反射动态设置导出的Excel列名 * * @param annotatedColumnName:实体类中被@Excel注解的字段名 * @param annotationFieldName:实体类中被@Excel中注解的属性名 * @param newAnnotationFieldValue:属性的新值 */
private void setExcelAnnotationValue(String annotatedColumnName, String annotationFieldName, String newAnnotationFieldValue){
try{
Class<AirQualityRankingResp> airQualityRankingRespClass = AirQualityRankingResp.class;
Field classDeclaredField = airQualityRankingRespClass.getDeclaredField(annotatedColumnName);
Excel excel = classDeclaredField.getAnnotation(Excel.class);
InvocationHandler excelInvocationHandler = Proxy.getInvocationHandler(excel);
Field excelInvocationHandlerField = excelInvocationHandler.getClass().getDeclaredField("memberValues");
excelInvocationHandlerField.setAccessible(true);
Map map = (Map) excelInvocationHandlerField.get(excelInvocationHandler);
map.put(annotationFieldName, newAnnotationFieldValue);
} catch (Exception e) {
e.printStackTrace();
}
}
- 使用示例
@RequestMapping(value = "/rankingAnalysisExportXls")
public ModelAndView exportXls(HttpServletRequest request, AirRankingReq airRankingReq) {
switch (airRankingReq.getName()){
case "2":
setExcelAnnotationValue("value","name","SO2");
break;
case "3":
setExcelAnnotationValue("value","name","NO2");
break;
case "4":
setExcelAnnotationValue("value","name","PM10");
break;
case "5":
setExcelAnnotationValue("value","name","CO");
break;
case "6":
setExcelAnnotationValue("value","name","O3");
break;
case "7":
setExcelAnnotationValue("value","name","PM2.5");
break;
}
List<AirQualityRankingResp> list = lrMonitorConcentrationService.rankingAnalysis(airRankingReq);
// Step.3 AutoPoi 导出Excel
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
mv.addObject(NormalExcelConstants.FILE_NAME, "lr_meteorological_data"); //此处设置的filename无效 ,前端会重更新设置一下
mv.addObject(NormalExcelConstants.CLASS, AirQualityRankingResp.class);
//update-begin--Author:liusq Date:20210126 for:图片导出报错,ImageBasePath未设置--------------------
ExportParams exportParams = new ExportParams("空气质量数据排名" + "报表", "导出人:" + sysUser.getRealname(), "空气质量排名");
exportParams.setImageBasePath(upLoadPath);
//update-end--Author:liusq Date:20210126 for:图片导出报错,ImageBasePath未设置----------------------
mv.addObject(NormalExcelConstants.PARAMS, exportParams);
mv.addObject(NormalExcelConstants.DATA_LIST, list);
return mv;
}

我这里是根据前端给的name值来确定生成表格的列名为什么,你们根据实际业务场景来进行判断,这里知道怎么用即可
边栏推荐
- DNA(脱氧核糖核酸)供应|碳纳米管载核酸-DNA/RNA材料|DNA/RNA核酸修饰磁性纳米颗粒
- Campus news release management system based on SSM
- How to implement Devops with automation tools | including low code and Devops application practice
- 李沐动手学深度学习V2-transformer和代码实现
- 二叉树--天然的查找语义(1)基础篇
- VIVO应用市场APP上架总结
- Shell programming specifications and variables
- Details of cross entropy loss function in pytorch
- 2022 0726 顾宇佳 学习笔记
- 如何借助自动化工具落地DevOps|含低代码与DevOps应用实践
猜你喜欢

内部类--看这篇就懂啦~

如何借助自动化工具落地DevOps|含低代码与DevOps应用实践

pytorch笔记:TD3

Image super-resolution evaluation index

Drools(5):Drools高级语法

基于SSM学生学籍管理系统

脱氧核糖核酸DNA修饰氧化锌|DNA修饰纳米金颗粒|DNA偶联修饰碳纳米材料

Drools(5):Drools基础语法(3)

指令集 x 数澜科技丨加速政企数字化转型,打造DT领域独角兽企业联盟

CdS quantum dots modified DNA | CDs DNA QDs | near infrared CdS quantum dots coupled DNA specification information
随机推荐
nvidia-smi 各参数意义
含有偶氮苯单体的肽核酸寡聚体(NH2-TNT4,N-PNAs)齐岳生物定制
Why can cross entropy loss be used to characterize loss
Watermelon book chapter 3 - linear model learning notes
指令集董事长潘爱民出席2022 ECUG Con,为中国技术力量发声
Netease Yunxin appeared at the giac global Internet architecture conference to decrypt the practice of the new generation of audio and video architecture in the meta universe scene
DNA科研实验应用|环糊精修饰核酸CD-RNA/DNA|环糊精核酸探针/量子点核酸探针
ZnS DNA QDs near infrared zinc sulfide ZnS quantum dots modified deoxyribonucleic acid dna|dna modified ZnS quantum dots
Analysis of pix2pix principle
2021 interview question of php+go for Zhongda factory (1)
MySQL limit paging query optimization practice
“蔚来杯“2022牛客暑期多校训练营1
基于SSM实现的校园新闻发布管理系统
基于SSM图书借阅管理系统
大疆livox定制的格式CustomMsg格式转换pointcloud2
PHP defines the array using commas,
肽核酸PNA-多肽PNA-TPP|Glt-Ala-Ala-Pro-Leu-pNA|Suc-Ala-Pro-pNA|Suc-AAPL-pNA|Suc-AAPM-pNA
Digital image processing -- Chapter 3 gray scale transformation and spatial filtering
Knowledge points and answers of PHP intermediate interview in 2020
Go obtains the processing results of goroutine through channel