|
@@ -22,10 +22,12 @@ import org.apache.poi.ss.usermodel.BorderStyle; |
|
@@ -22,10 +22,12 @@ import org.apache.poi.ss.usermodel.BorderStyle; |
|
22
|
import org.apache.poi.ss.usermodel.Cell;
|
22
|
import org.apache.poi.ss.usermodel.Cell;
|
|
23
|
import org.apache.poi.ss.usermodel.CellStyle;
|
23
|
import org.apache.poi.ss.usermodel.CellStyle;
|
|
24
|
import org.apache.poi.ss.usermodel.CellType;
|
24
|
import org.apache.poi.ss.usermodel.CellType;
|
|
|
|
25
|
+import org.apache.poi.ss.usermodel.ClientAnchor;
|
|
25
|
import org.apache.poi.ss.usermodel.DataValidation;
|
26
|
import org.apache.poi.ss.usermodel.DataValidation;
|
|
26
|
import org.apache.poi.ss.usermodel.DataValidationConstraint;
|
27
|
import org.apache.poi.ss.usermodel.DataValidationConstraint;
|
|
27
|
import org.apache.poi.ss.usermodel.DataValidationHelper;
|
28
|
import org.apache.poi.ss.usermodel.DataValidationHelper;
|
|
28
|
import org.apache.poi.ss.usermodel.DateUtil;
|
29
|
import org.apache.poi.ss.usermodel.DateUtil;
|
|
|
|
30
|
+import org.apache.poi.ss.usermodel.Drawing;
|
|
29
|
import org.apache.poi.ss.usermodel.FillPatternType;
|
31
|
import org.apache.poi.ss.usermodel.FillPatternType;
|
|
30
|
import org.apache.poi.ss.usermodel.Font;
|
32
|
import org.apache.poi.ss.usermodel.Font;
|
|
31
|
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
33
|
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
|
@@ -37,6 +39,7 @@ import org.apache.poi.ss.usermodel.Workbook; |
|
@@ -37,6 +39,7 @@ import org.apache.poi.ss.usermodel.Workbook; |
|
37
|
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
39
|
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
|
38
|
import org.apache.poi.ss.util.CellRangeAddressList;
|
40
|
import org.apache.poi.ss.util.CellRangeAddressList;
|
|
39
|
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
41
|
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
|
|
|
42
|
+import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
|
|
40
|
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
|
43
|
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
|
|
41
|
import org.slf4j.Logger;
|
44
|
import org.slf4j.Logger;
|
|
42
|
import org.slf4j.LoggerFactory;
|
45
|
import org.slf4j.LoggerFactory;
|
|
@@ -51,6 +54,8 @@ import com.ruoyi.common.exception.CustomException; |
|
@@ -51,6 +54,8 @@ import com.ruoyi.common.exception.CustomException; |
|
51
|
import com.ruoyi.common.utils.DateUtils;
|
54
|
import com.ruoyi.common.utils.DateUtils;
|
|
52
|
import com.ruoyi.common.utils.DictUtils;
|
55
|
import com.ruoyi.common.utils.DictUtils;
|
|
53
|
import com.ruoyi.common.utils.StringUtils;
|
56
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
|
57
|
+import com.ruoyi.common.utils.file.FileTypeUtils;
|
|
|
|
58
|
+import com.ruoyi.common.utils.file.ImageUtils;
|
|
54
|
import com.ruoyi.common.utils.reflect.ReflectUtils;
|
59
|
import com.ruoyi.common.utils.reflect.ReflectUtils;
|
|
55
|
|
60
|
|
|
56
|
/**
|
61
|
/**
|
|
@@ -103,6 +108,11 @@ public class ExcelUtil<T> |
|
@@ -103,6 +108,11 @@ public class ExcelUtil<T> |
|
103
|
private List<Object[]> fields;
|
108
|
private List<Object[]> fields;
|
|
104
|
|
109
|
|
|
105
|
/**
|
110
|
/**
|
|
|
|
111
|
+ * 最大高度
|
|
|
|
112
|
+ */
|
|
|
|
113
|
+ private short maxHeight;
|
|
|
|
114
|
+
|
|
|
|
115
|
+ /**
|
|
106
|
* 统计列表
|
116
|
* 统计列表
|
|
107
|
*/
|
117
|
*/
|
|
108
|
private Map<Integer, Double> statistics = new HashMap<Integer, Double>();
|
118
|
private Map<Integer, Double> statistics = new HashMap<Integer, Double>();
|
|
@@ -239,9 +249,17 @@ public class ExcelUtil<T> |
|
@@ -239,9 +249,17 @@ public class ExcelUtil<T> |
|
239
|
}
|
249
|
}
|
|
240
|
else
|
250
|
else
|
|
241
|
{
|
251
|
{
|
|
|
|
252
|
+ String dateFormat = field.getAnnotation(Excel.class).dateFormat();
|
|
|
|
253
|
+ if (StringUtils.isNotEmpty(dateFormat))
|
|
|
|
254
|
+ {
|
|
|
|
255
|
+ val = DateUtils.parseDateToStr(dateFormat, (Date) val);
|
|
|
|
256
|
+ }
|
|
|
|
257
|
+ else
|
|
|
|
258
|
+ {
|
|
242
|
val = Convert.toStr(val);
|
259
|
val = Convert.toStr(val);
|
|
243
|
}
|
260
|
}
|
|
244
|
}
|
261
|
}
|
|
|
|
262
|
+ }
|
|
245
|
else if ((Integer.TYPE == fieldType || Integer.class == fieldType) && StringUtils.isNumeric(Convert.toStr(val)))
|
263
|
else if ((Integer.TYPE == fieldType || Integer.class == fieldType) && StringUtils.isNumeric(Convert.toStr(val)))
|
|
246
|
{
|
264
|
{
|
|
247
|
val = Convert.toInt(val);
|
265
|
val = Convert.toInt(val);
|
|
@@ -521,6 +539,47 @@ public class ExcelUtil<T> |
|
@@ -521,6 +539,47 @@ public class ExcelUtil<T> |
|
521
|
{
|
539
|
{
|
|
522
|
cell.setCellValue(StringUtils.contains(Convert.toStr(value), ".") ? Convert.toDouble(value) : Convert.toInt(value));
|
540
|
cell.setCellValue(StringUtils.contains(Convert.toStr(value), ".") ? Convert.toDouble(value) : Convert.toInt(value));
|
|
523
|
}
|
541
|
}
|
|
|
|
542
|
+ else if (ColumnType.IMAGE == attr.cellType())
|
|
|
|
543
|
+ {
|
|
|
|
544
|
+ ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1),
|
|
|
|
545
|
+ cell.getRow().getRowNum() + 1);
|
|
|
|
546
|
+ String imagePath = Convert.toStr(value);
|
|
|
|
547
|
+ if (StringUtils.isNotEmpty(imagePath))
|
|
|
|
548
|
+ {
|
|
|
|
549
|
+ byte[] data = ImageUtils.getImage(imagePath);
|
|
|
|
550
|
+ getDrawingPatriarch(cell.getSheet()).createPicture(anchor,
|
|
|
|
551
|
+ cell.getSheet().getWorkbook().addPicture(data, getImageType(data)));
|
|
|
|
552
|
+ }
|
|
|
|
553
|
+ }
|
|
|
|
554
|
+ }
|
|
|
|
555
|
+
|
|
|
|
556
|
+ /**
|
|
|
|
557
|
+ * 获取画布
|
|
|
|
558
|
+ */
|
|
|
|
559
|
+ public static Drawing<?> getDrawingPatriarch(Sheet sheet)
|
|
|
|
560
|
+ {
|
|
|
|
561
|
+ if (sheet.getDrawingPatriarch() == null)
|
|
|
|
562
|
+ {
|
|
|
|
563
|
+ sheet.createDrawingPatriarch();
|
|
|
|
564
|
+ }
|
|
|
|
565
|
+ return sheet.getDrawingPatriarch();
|
|
|
|
566
|
+ }
|
|
|
|
567
|
+
|
|
|
|
568
|
+ /**
|
|
|
|
569
|
+ * 获取图片类型,设置图片插入类型
|
|
|
|
570
|
+ */
|
|
|
|
571
|
+ public int getImageType(byte[] value)
|
|
|
|
572
|
+ {
|
|
|
|
573
|
+ String type = FileTypeUtils.getFileExtendName(value);
|
|
|
|
574
|
+ if ("JPG".equalsIgnoreCase(type))
|
|
|
|
575
|
+ {
|
|
|
|
576
|
+ return Workbook.PICTURE_TYPE_JPEG;
|
|
|
|
577
|
+ }
|
|
|
|
578
|
+ else if ("PNG".equalsIgnoreCase(type))
|
|
|
|
579
|
+ {
|
|
|
|
580
|
+ return Workbook.PICTURE_TYPE_PNG;
|
|
|
|
581
|
+ }
|
|
|
|
582
|
+ return Workbook.PICTURE_TYPE_JPEG;
|
|
524
|
}
|
583
|
}
|
|
525
|
|
584
|
|
|
526
|
/**
|
585
|
/**
|
|
@@ -536,7 +595,6 @@ public class ExcelUtil<T> |
|
@@ -536,7 +595,6 @@ public class ExcelUtil<T> |
|
536
|
{
|
595
|
{
|
|
537
|
// 设置列宽
|
596
|
// 设置列宽
|
|
538
|
sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256));
|
597
|
sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256));
|
|
539
|
- row.setHeight((short) (attr.height() * 20));
|
|
|
|
540
|
}
|
598
|
}
|
|
541
|
// 如果设置了提示信息则鼠标放上去提示.
|
599
|
// 如果设置了提示信息则鼠标放上去提示.
|
|
542
|
if (StringUtils.isNotEmpty(attr.prompt()))
|
600
|
if (StringUtils.isNotEmpty(attr.prompt()))
|
|
@@ -561,7 +619,7 @@ public class ExcelUtil<T> |
|
@@ -561,7 +619,7 @@ public class ExcelUtil<T> |
|
561
|
try
|
619
|
try
|
|
562
|
{
|
620
|
{
|
|
563
|
// 设置行高
|
621
|
// 设置行高
|
|
564
|
- row.setHeight((short) (attr.height() * 20));
|
622
|
+ row.setHeight(maxHeight);
|
|
565
|
// 根据Excel中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列.
|
623
|
// 根据Excel中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列.
|
|
566
|
if (attr.isExport())
|
624
|
if (attr.isExport())
|
|
567
|
{
|
625
|
{
|
|
@@ -916,6 +974,21 @@ public class ExcelUtil<T> |
|
@@ -916,6 +974,21 @@ public class ExcelUtil<T> |
|
916
|
}
|
974
|
}
|
|
917
|
}
|
975
|
}
|
|
918
|
this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
|
976
|
this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
|
|
|
|
977
|
+ this.maxHeight = getRowHeight();
|
|
|
|
978
|
+ }
|
|
|
|
979
|
+
|
|
|
|
980
|
+ /**
|
|
|
|
981
|
+ * 根据注解获取最大行高
|
|
|
|
982
|
+ */
|
|
|
|
983
|
+ public short getRowHeight()
|
|
|
|
984
|
+ {
|
|
|
|
985
|
+ double maxHeight = 0;
|
|
|
|
986
|
+ for (Object[] os : this.fields)
|
|
|
|
987
|
+ {
|
|
|
|
988
|
+ Excel excel = (Excel) os[1];
|
|
|
|
989
|
+ maxHeight = maxHeight > excel.height() ? maxHeight : excel.height();
|
|
|
|
990
|
+ }
|
|
|
|
991
|
+ return (short) (maxHeight * 20);
|
|
919
|
}
|
992
|
}
|
|
920
|
|
993
|
|
|
921
|
/**
|
994
|
/**
|