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,6 +10,8 @@ import cn.iocoder.yudao.framework.common.core.KeyValue; | ||
| 10 | import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils; | 10 | import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils; |
| 11 | import cn.iocoder.yudao.framework.excel.core.annotations.ExcelColumnSelect; | 11 | import cn.iocoder.yudao.framework.excel.core.annotations.ExcelColumnSelect; |
| 12 | import cn.iocoder.yudao.framework.excel.core.function.ExcelColumnSelectFunction; | 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 | import com.alibaba.excel.annotation.ExcelProperty; | 15 | import com.alibaba.excel.annotation.ExcelProperty; |
| 14 | import com.alibaba.excel.write.handler.SheetWriteHandler; | 16 | import com.alibaba.excel.write.handler.SheetWriteHandler; |
| 15 | import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; | 17 | import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; |
| @@ -20,6 +22,7 @@ import org.apache.poi.ss.usermodel.*; | @@ -20,6 +22,7 @@ import org.apache.poi.ss.usermodel.*; | ||
| 20 | import org.apache.poi.ss.util.CellRangeAddressList; | 22 | import org.apache.poi.ss.util.CellRangeAddressList; |
| 21 | 23 | ||
| 22 | import java.lang.reflect.Field; | 24 | import java.lang.reflect.Field; |
| 25 | +import java.lang.reflect.Modifier; | ||
| 23 | import java.util.Comparator; | 26 | import java.util.Comparator; |
| 24 | import java.util.HashMap; | 27 | import java.util.HashMap; |
| 25 | import java.util.List; | 28 | import java.util.List; |
| @@ -56,7 +59,17 @@ public class SelectSheetWriteHandler implements SheetWriteHandler { | @@ -56,7 +59,17 @@ public class SelectSheetWriteHandler implements SheetWriteHandler { | ||
| 56 | public SelectSheetWriteHandler(Class<?> head) { | 59 | public SelectSheetWriteHandler(Class<?> head) { |
| 57 | // 解析下拉数据 | 60 | // 解析下拉数据 |
| 58 | int colIndex = 0; | 61 | int colIndex = 0; |
| 62 | + boolean ignoreUnannotated = head.isAnnotationPresent(ExcelIgnoreUnannotated.class); | ||
| 59 | for (Field field : head.getDeclaredFields()) { | 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 | if (field.isAnnotationPresent(ExcelColumnSelect.class)) { | 73 | if (field.isAnnotationPresent(ExcelColumnSelect.class)) { |
| 61 | ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); | 74 | ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); |
| 62 | if (excelProperty != null && excelProperty.index() != -1) { | 75 | if (excelProperty != null && excelProperty.index() != -1) { |
| @@ -69,6 +82,18 @@ public class SelectSheetWriteHandler implements SheetWriteHandler { | @@ -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 | * 获得下拉数据,并添加到 {@link #selectMap} 中 | 97 | * 获得下拉数据,并添加到 {@link #selectMap} 中 |
| 73 | * | 98 | * |
| 74 | * @param colIndex 列索引 | 99 | * @param colIndex 列索引 |