当前位置:网站首页>Use reflection to dynamically modify annotation attributes of @excel
Use reflection to dynamically modify annotation attributes of @excel
2022-07-27 07:20:00 【Son is not my fish】
Business scenario : We use poi When exporting data , Usually according to @Excel(name=“xxx”) To determine the column name . Usually, this will not change . But here are a few cases , When we need to make changes here according to certain situations , We need to use reflection .
AirQualityRankingResp.java
@Data
public class AirQualityRankingResp {
/** Site name */
@Excel(name = " Site name ")
private String site;
private String siteNumber;
@Excel(name = " Analysis time ")
private String dateTime;
/** Pollution factor */
private String pollutionFactor;
@Excel(name = "AQI")
private Double value;
/** ranking */
@Excel(name = " ranking ")
private Integer ranking;
}
Normal use @Excel The exported data is as follows

Dynamic modification @Excel Properties of , Code implementation
/** * Exported through reflection dynamic settings Excel Name * * @param annotatedColumnName: Entity class is @Excel The field name of the annotation * @param annotationFieldName: Entity class is @Excel Attribute name of the annotation in * @param newAnnotationFieldValue: New value of property */
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();
}
}
- Examples of use
@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 export Excel
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
mv.addObject(NormalExcelConstants.FILE_NAME, "lr_meteorological_data"); // Set here filename Invalid , The front end will be updated and set again
mv.addObject(NormalExcelConstants.CLASS, AirQualityRankingResp.class);
//update-begin--Author:liusq Date:20210126 for: Image export error ,ImageBasePath Not set --------------------
ExportParams exportParams = new ExportParams(" Ranking of air quality data " + " report form ", " Exporter :" + sysUser.getRealname(), " Air quality ranking ");
exportParams.setImageBasePath(upLoadPath);
//update-end--Author:liusq Date:20210126 for: Image export error ,ImageBasePath Not set ----------------------
mv.addObject(NormalExcelConstants.PARAMS, exportParams);
mv.addObject(NormalExcelConstants.DATA_LIST, list);
return mv;
}

I give it here according to the front end name Value to determine the column name of the generated table , You judge according to the actual business scenario , Just know how to use it here
边栏推荐
- MySQL index failure and solution practice
- 请教大佬们一个问题,pgsqlcdc任务运行一段时间就不能监测变化了,重启就可以了,这个该从哪方面入
- Jmeter: interface automation test - BeanShell compares database data and return data
- Gbase 8C - SQL reference 6 SQL syntax (13)
- Codeforces Round #809 (Div. 2)(6/6)(Kruskal重构树)
- oracle的触发器的使用举例
- String类的用法
- 零号培训平台课程-2、SSRF基础
- 35. Search insert position
- Codeforces Round #787 (Div. 3)(7/7)
猜你喜欢
随机推荐
Gbase 8C - SQL reference 6 SQL syntax (13)
Algorithm -- Fibonacci sequence (kotlin)
DRConv-pytorch改称输出和输入一样的尺寸
在rhel7.3中编译和使用log4cxx
Overall dichotomy?
Codeworks round 809 (Div. 2) (6/6) (Kruskal reconstruction tree)
LogCat工具
tigervnc的使用
Gbase 8C product introduction
oracle清理含有引用分区的表的数据库磁盘空间
请问有人使用oracle xstream 时出现个别capture延迟很大的吗,该如何解决延迟问题呢
Want to sink into redis hash and write in the attributes and values of the object. Do the bosses have a demo?
35. Search insert position
VIM editor deletes all file contents
算法--斐波那契数列(Kotlin)
Codeforces Round #809 (Div. 2)(6/6)(Kruskal重构树)
Drools(5):Drools基础语法(3)
MySQL optimization SQL related (continuous supplement)
flink cdc 抽取oracle的数据,会不断的占用oracle的内存吗,最后引发ora-040
2021 interview question of php+go for Zhongda factory (1)









