poi 导出 name属性自定义、隐藏列
实体类
package com.ruoyi.logics.repairrecord.vo; import com.ruoyi.common.core.annotation.Excel; import com.ruoyi.common.core.web.domain.BaseEntity; /** * 维修统计 */ public class RepairRecordStatisticsRequest extends BaseEntity { /** * 统计名称 */ @Excel(name = "统计名称") private String name; /** * 维修统计数量 */ @Excel(name = "工单数量") private String count; /** * 未完成数量 */ @Excel(name = "未完成数量") private String noFinishedCount; /** * 已完成数量 */ @Excel(name = "已完成数量") private String finishedCount; /** * 完成率 */ @Excel(name = "完成率", suffix = "%") private String finishedRete; /** * 查询条件:维修班组 * @return */ private String repairTeamCode; /** * 查询条件:维修项目 * @return */ private String repairProjectCode; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCount() { return count; } public void setCount(String count) { this.count = count; } public String getRepairTeamCode() { return repairTeamCode; } public void setRepairTeamCode(String repairTeamCode) { this.repairTeamCode = repairTeamCode; } public String getRepairProjectCode() { return repairProjectCode; } public void setRepairProjectCode(String repairProjectCode) { this.repairProjectCode = repairProjectCode; } }
使用
/** * 维修项目统计导出 */ @Log(title = "维修项目统计导出", businessType = BusinessType.EXPORT) @PostMapping("/exportStatisticsByProject") public void exportStatisticsByProject(HttpServletResponse response, RepairRecordStatisticsRequest repairRecordStatisticsRequest) { List<RepairRecordStatisticsRequest> list = repairRecordService.statisticsListByProject(repairRecordStatisticsRequest); // 将name属性导出值显示为“维修项目” this.setExcelAnnotationValue("name", "name", "维修项目"); ExcelUtil<RepairRecordStatisticsRequest> util = new ExcelUtil<RepairRecordStatisticsRequest>(RepairRecordStatisticsRequest.class); // 隐藏列,只导出名称和数量 util.hideColumn("noFinishedCount", "finishedCount", "finishedRete"); util.exportExcel(response, list, "维修项目统计"); }
方法
/** * 通过反射动态设置导出的Excel列名 * * @param annotatedColumnName:实体类中被@Excel注解的字段名 * @param annotationFieldName:实体类中被@Excel中注解的属性名 * @param newAnnotationFieldValue:属性的新值 */ private void setExcelAnnotationValue(String annotatedColumnName, String annotationFieldName, String newAnnotationFieldValue) { try { Class<RepairRecordStatisticsRequest> airQualityRankingRespClass = RepairRecordStatisticsRequest.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(); } }