Commit 4dca326081d6d1d051b4fa0c59ea31465b737cee
Merge branch 'master-jdk17' of https://github.com/YunaiV/ruoyi-vue-pro into master-jdk17
Showing
1 changed file
with
25 additions
and
0 deletions
yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/excel/core/handler/SelectSheetWriteHandler.java
| ... | ... | @@ -10,6 +10,8 @@ import cn.iocoder.yudao.framework.common.core.KeyValue; |
| 10 | 10 | import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils; |
| 11 | 11 | import cn.iocoder.yudao.framework.excel.core.annotations.ExcelColumnSelect; |
| 12 | 12 | import cn.iocoder.yudao.framework.excel.core.function.ExcelColumnSelectFunction; |
| 13 | +import com.alibaba.excel.annotation.ExcelIgnore; | |
| 14 | +import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; | |
| 13 | 15 | import com.alibaba.excel.annotation.ExcelProperty; |
| 14 | 16 | import com.alibaba.excel.write.handler.SheetWriteHandler; |
| 15 | 17 | import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; |
| ... | ... | @@ -20,6 +22,7 @@ import org.apache.poi.ss.usermodel.*; |
| 20 | 22 | import org.apache.poi.ss.util.CellRangeAddressList; |
| 21 | 23 | |
| 22 | 24 | import java.lang.reflect.Field; |
| 25 | +import java.lang.reflect.Modifier; | |
| 23 | 26 | import java.util.Comparator; |
| 24 | 27 | import java.util.HashMap; |
| 25 | 28 | import java.util.List; |
| ... | ... | @@ -56,7 +59,17 @@ public class SelectSheetWriteHandler implements SheetWriteHandler { |
| 56 | 59 | public SelectSheetWriteHandler(Class<?> head) { |
| 57 | 60 | // 解析下拉数据 |
| 58 | 61 | int colIndex = 0; |
| 62 | + boolean ignoreUnannotated = head.isAnnotationPresent(ExcelIgnoreUnannotated.class); | |
| 59 | 63 | for (Field field : head.getDeclaredFields()) { |
| 64 | + // 忽略 static final 或 transient 的字段 | |
| 65 | + if(isStaticFinalOrTransient(field) ) { | |
| 66 | + continue; | |
| 67 | + } | |
| 68 | + // 忽略的字段跳过 | |
| 69 | + if((ignoreUnannotated && !field.isAnnotationPresent(ExcelProperty.class)) | |
| 70 | + || field.isAnnotationPresent(ExcelIgnore.class)){ | |
| 71 | + continue; | |
| 72 | + } | |
| 60 | 73 | if (field.isAnnotationPresent(ExcelColumnSelect.class)) { |
| 61 | 74 | ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); |
| 62 | 75 | if (excelProperty != null && excelProperty.index() != -1) { |
| ... | ... | @@ -69,6 +82,18 @@ public class SelectSheetWriteHandler implements SheetWriteHandler { |
| 69 | 82 | } |
| 70 | 83 | |
| 71 | 84 | /** |
| 85 | + * 判断字段是否是静态的、最终的、 transient 的 | |
| 86 | + * EasyExcel 默认是忽略 static final 或 transient 的字段,所以需要判断 | |
| 87 | + * @param field 字段 | |
| 88 | + * @return 是否是静态的、最终的、 transient 的 | |
| 89 | + */ | |
| 90 | + private boolean isStaticFinalOrTransient(Field field) { | |
| 91 | + return (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) | |
| 92 | + || Modifier.isTransient(field.getModifiers()); | |
| 93 | + } | |
| 94 | + | |
| 95 | + | |
| 96 | + /** | |
| 72 | 97 | * 获得下拉数据,并添加到 {@link #selectMap} 中 |
| 73 | 98 | * |
| 74 | 99 | * @param colIndex 列索引 | ... | ... |