|
@@ -39,6 +39,7 @@ import org.apache.poi.ss.usermodel.Cell; |
|
@@ -39,6 +39,7 @@ import org.apache.poi.ss.usermodel.Cell; |
|
39
|
import org.apache.poi.ss.usermodel.CellStyle;
|
39
|
import org.apache.poi.ss.usermodel.CellStyle;
|
|
40
|
import org.apache.poi.ss.usermodel.CellType;
|
40
|
import org.apache.poi.ss.usermodel.CellType;
|
|
41
|
import org.apache.poi.ss.usermodel.ClientAnchor;
|
41
|
import org.apache.poi.ss.usermodel.ClientAnchor;
|
|
|
|
42
|
+import org.apache.poi.ss.usermodel.DataFormat;
|
|
42
|
import org.apache.poi.ss.usermodel.DataValidation;
|
43
|
import org.apache.poi.ss.usermodel.DataValidation;
|
|
43
|
import org.apache.poi.ss.usermodel.DataValidationConstraint;
|
44
|
import org.apache.poi.ss.usermodel.DataValidationConstraint;
|
|
44
|
import org.apache.poi.ss.usermodel.DataValidationHelper;
|
45
|
import org.apache.poi.ss.usermodel.DataValidationHelper;
|
|
@@ -783,6 +784,8 @@ public class ExcelUtil<T> |
|
@@ -783,6 +784,8 @@ public class ExcelUtil<T> |
|
783
|
titleFont.setFontHeightInPoints((short) 16);
|
784
|
titleFont.setFontHeightInPoints((short) 16);
|
|
784
|
titleFont.setBold(true);
|
785
|
titleFont.setBold(true);
|
|
785
|
style.setFont(titleFont);
|
786
|
style.setFont(titleFont);
|
|
|
|
787
|
+ DataFormat dataFormat = wb.createDataFormat();
|
|
|
|
788
|
+ style.setDataFormat(dataFormat.getFormat("@"));
|
|
786
|
styles.put("title", style);
|
789
|
styles.put("title", style);
|
|
787
|
|
790
|
|
|
788
|
style = wb.createCellStyle();
|
791
|
style = wb.createCellStyle();
|
|
@@ -862,8 +865,37 @@ public class ExcelUtil<T> |
|
@@ -862,8 +865,37 @@ public class ExcelUtil<T> |
|
862
|
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
|
865
|
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
|
|
863
|
for (Object[] os : fields)
|
866
|
for (Object[] os : fields)
|
|
864
|
{
|
867
|
{
|
|
|
|
868
|
+ Field field = (Field) os[0];
|
|
865
|
Excel excel = (Excel) os[1];
|
869
|
Excel excel = (Excel) os[1];
|
|
866
|
- String key = StringUtils.format("data_{}_{}_{}", excel.align(), excel.color(), excel.backgroundColor());
|
870
|
+ if (Collection.class.isAssignableFrom(field.getType()))
|
|
|
|
871
|
+ {
|
|
|
|
872
|
+ ParameterizedType pt = (ParameterizedType) field.getGenericType();
|
|
|
|
873
|
+ Class<?> subClass = (Class<?>) pt.getActualTypeArguments()[0];
|
|
|
|
874
|
+ List<Field> subFields = FieldUtils.getFieldsListWithAnnotation(subClass, Excel.class);
|
|
|
|
875
|
+ for (Field subField : subFields)
|
|
|
|
876
|
+ {
|
|
|
|
877
|
+ Excel subExcel = subField.getAnnotation(Excel.class);
|
|
|
|
878
|
+ annotationDataStyles(styles, subField, subExcel);
|
|
|
|
879
|
+ }
|
|
|
|
880
|
+ }
|
|
|
|
881
|
+ else
|
|
|
|
882
|
+ {
|
|
|
|
883
|
+ annotationDataStyles(styles, field, excel);
|
|
|
|
884
|
+ }
|
|
|
|
885
|
+ }
|
|
|
|
886
|
+ return styles;
|
|
|
|
887
|
+ }
|
|
|
|
888
|
+
|
|
|
|
889
|
+ /**
|
|
|
|
890
|
+ * 根据Excel注解创建表格列样式
|
|
|
|
891
|
+ *
|
|
|
|
892
|
+ * @param styles 自定义样式列表
|
|
|
|
893
|
+ * @param field 属性列信息
|
|
|
|
894
|
+ * @param excel 注解信息
|
|
|
|
895
|
+ */
|
|
|
|
896
|
+ public void annotationDataStyles(Map<String, CellStyle> styles, Field field, Excel excel)
|
|
|
|
897
|
+ {
|
|
|
|
898
|
+ String key = StringUtils.format("data_{}_{}_{}_{}", excel.align(), excel.color(), excel.backgroundColor(), excel.cellType());
|
|
867
|
if (!styles.containsKey(key))
|
899
|
if (!styles.containsKey(key))
|
|
868
|
{
|
900
|
{
|
|
869
|
CellStyle style = wb.createCellStyle();
|
901
|
CellStyle style = wb.createCellStyle();
|
|
@@ -884,10 +916,13 @@ public class ExcelUtil<T> |
|
@@ -884,10 +916,13 @@ public class ExcelUtil<T> |
|
884
|
dataFont.setFontHeightInPoints((short) 10);
|
916
|
dataFont.setFontHeightInPoints((short) 10);
|
|
885
|
dataFont.setColor(excel.color().index);
|
917
|
dataFont.setColor(excel.color().index);
|
|
886
|
style.setFont(dataFont);
|
918
|
style.setFont(dataFont);
|
|
887
|
- styles.put(key, style);
|
919
|
+ if (ColumnType.TEXT == excel.cellType())
|
|
|
|
920
|
+ {
|
|
|
|
921
|
+ DataFormat dataFormat = wb.createDataFormat();
|
|
|
|
922
|
+ style.setDataFormat(dataFormat.getFormat("@"));
|
|
888
|
}
|
923
|
}
|
|
|
|
924
|
+ styles.put(key, style);
|
|
889
|
}
|
925
|
}
|
|
890
|
- return styles;
|
|
|
|
891
|
}
|
926
|
}
|
|
892
|
|
927
|
|
|
893
|
/**
|
928
|
/**
|
|
@@ -904,7 +939,7 @@ public class ExcelUtil<T> |
|
@@ -904,7 +939,7 @@ public class ExcelUtil<T> |
|
904
|
if (isSubList())
|
939
|
if (isSubList())
|
|
905
|
{
|
940
|
{
|
|
906
|
// 填充默认样式,防止合并单元格样式失效
|
941
|
// 填充默认样式,防止合并单元格样式失效
|
|
907
|
- sheet.setDefaultColumnStyle(column, styles.get(StringUtils.format("data_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor())));
|
942
|
+ sheet.setDefaultColumnStyle(column, styles.get(StringUtils.format("data_{}_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor(), attr.cellType())));
|
|
908
|
if (attr.needMerge())
|
943
|
if (attr.needMerge())
|
|
909
|
{
|
944
|
{
|
|
910
|
sheet.addMergedRegion(new CellRangeAddress(rownum - 1, rownum, column, column));
|
945
|
sheet.addMergedRegion(new CellRangeAddress(rownum - 1, rownum, column, column));
|
|
@@ -922,7 +957,7 @@ public class ExcelUtil<T> |
|
@@ -922,7 +957,7 @@ public class ExcelUtil<T> |
|
922
|
*/
|
957
|
*/
|
|
923
|
public void setCellVo(Object value, Excel attr, Cell cell)
|
958
|
public void setCellVo(Object value, Excel attr, Cell cell)
|
|
924
|
{
|
959
|
{
|
|
925
|
- if (ColumnType.STRING == attr.cellType())
|
960
|
+ if (ColumnType.STRING == attr.cellType() || ColumnType.TEXT == attr.cellType())
|
|
926
|
{
|
961
|
{
|
|
927
|
String cellValue = Convert.toStr(value);
|
962
|
String cellValue = Convert.toStr(value);
|
|
928
|
// 对于任何以表达式触发字符 =-+@开头的单元格,直接使用tab字符作为前缀,防止CSV注入。
|
963
|
// 对于任何以表达式触发字符 =-+@开头的单元格,直接使用tab字符作为前缀,防止CSV注入。
|
|
@@ -1034,7 +1069,7 @@ public class ExcelUtil<T> |
|
@@ -1034,7 +1069,7 @@ public class ExcelUtil<T> |
|
1034
|
CellRangeAddress cellAddress = new CellRangeAddress(subMergedFirstRowNum, subMergedLastRowNum, column, column);
|
1069
|
CellRangeAddress cellAddress = new CellRangeAddress(subMergedFirstRowNum, subMergedLastRowNum, column, column);
|
|
1035
|
sheet.addMergedRegion(cellAddress);
|
1070
|
sheet.addMergedRegion(cellAddress);
|
|
1036
|
}
|
1071
|
}
|
|
1037
|
- cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor())));
|
1072
|
+ cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor(), attr.cellType())));
|
|
1038
|
|
1073
|
|
|
1039
|
// 用于读取对象中的属性
|
1074
|
// 用于读取对象中的属性
|
|
1040
|
Object value = getTargetValue(vo, field, attr);
|
1075
|
Object value = getTargetValue(vo, field, attr);
|