|
|
@@ -1,135 +0,0 @@
|
|
1
|
|
-package com.zzsmart.qomo.kn.cost.manage.util;
|
|
2
|
|
-
|
|
3
|
|
-import com.alibaba.excel.EasyExcel;
|
|
4
|
|
-import com.alibaba.excel.annotation.ExcelProperty;
|
|
5
|
|
-import com.alibaba.excel.write.metadata.style.WriteCellStyle;
|
|
6
|
|
-import com.alibaba.excel.write.metadata.style.WriteFont;
|
|
7
|
|
-import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
|
|
8
|
|
-import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostStandardDetail;
|
|
9
|
|
-import org.apache.poi.ss.usermodel.*;
|
|
10
|
|
-
|
|
11
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
12
|
|
-import java.io.IOException;
|
|
13
|
|
-import java.io.UnsupportedEncodingException;
|
|
14
|
|
-import java.lang.reflect.Field;
|
|
15
|
|
-import java.net.URLEncoder;
|
|
16
|
|
-import java.nio.charset.StandardCharsets;
|
|
17
|
|
-import java.util.ArrayList;
|
|
18
|
|
-import java.util.HashSet;
|
|
19
|
|
-import java.util.List;
|
|
20
|
|
-import java.util.Set;
|
|
21
|
|
-
|
|
22
|
|
-public class EasyExcelUtil {
|
|
23
|
|
- /**
|
|
24
|
|
- * 导出Excel文件到HTTP响应流,包含自定义表头。
|
|
25
|
|
- *
|
|
26
|
|
- * @param response HTTP响应对象
|
|
27
|
|
- * @param fileName 导出的文件名
|
|
28
|
|
- * @param head 表头信息列表
|
|
29
|
|
- * @param data 需要导出的数据列表
|
|
30
|
|
- * @param clazz 数据模型的Class类型
|
|
31
|
|
- * @throws IOException IO异常
|
|
32
|
|
- */
|
|
33
|
|
- public static <T> void export(HttpServletResponse response, String fileName, List<?> head, List<T> data, Class<T> clazz) throws IOException {
|
|
34
|
|
- response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
35
|
|
- response.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
|
36
|
|
- try {
|
|
37
|
|
- fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()).replaceAll("\\+", "%20");
|
|
38
|
|
- response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
|
39
|
|
- } catch (UnsupportedEncodingException e) {
|
|
40
|
|
- e.printStackTrace();
|
|
41
|
|
- }
|
|
42
|
|
-
|
|
43
|
|
- // 自定义表头样式
|
|
44
|
|
- HorizontalCellStyleStrategy horizontalCellStyleStrategy = getHorizontalCellStyleStrategy();
|
|
45
|
|
-
|
|
46
|
|
- // 使用EasyExcel导出
|
|
47
|
|
- try {
|
|
48
|
|
- EasyExcel.write(response.getOutputStream(), clazz)
|
|
49
|
|
- // 注册样式策略
|
|
50
|
|
- .registerWriteHandler(horizontalCellStyleStrategy).sheet("1111").doWrite(data);
|
|
51
|
|
- } catch (IOException e) {
|
|
52
|
|
- e.printStackTrace();
|
|
53
|
|
- throw new RuntimeException(e);
|
|
54
|
|
- }
|
|
55
|
|
-// FileOutputStream outputStream= new FileOutputStream(fileName);
|
|
56
|
|
-// try {
|
|
57
|
|
-// EasyExcel.write(outputStream)
|
|
58
|
|
-// // 设置自定义样式,可选
|
|
59
|
|
-// .sheet("Sheet1") // 设置工作表名称
|
|
60
|
|
-// .doWrite(data); // 将数据写入Excel
|
|
61
|
|
-// } catch (Exception e) {
|
|
62
|
|
-// e.printStackTrace();
|
|
63
|
|
-// System.out.println("zheli==============");
|
|
64
|
|
-// } finally {
|
|
65
|
|
-//// if (excelWriter != null) {
|
|
66
|
|
-//// excelWriter.finish();
|
|
67
|
|
-//// }
|
|
68
|
|
-// if (outputStream != null) {
|
|
69
|
|
-// outputStream.close();
|
|
70
|
|
-// }
|
|
71
|
|
-
|
|
72
|
|
-// }
|
|
73
|
|
- }
|
|
74
|
|
-
|
|
75
|
|
- /**
|
|
76
|
|
- * 创建自定义的水平单元格样式策略,用于表头样式。
|
|
77
|
|
- *
|
|
78
|
|
- * @return 水平单元格样式策略
|
|
79
|
|
- */
|
|
80
|
|
- private static HorizontalCellStyleStrategy getHorizontalCellStyleStrategy() {
|
|
81
|
|
- // 设置表头字体样式
|
|
82
|
|
- WriteFont headFont = new WriteFont();
|
|
83
|
|
- headFont.setFontHeightInPoints((short) 12);
|
|
84
|
|
- headFont.setFontName("Arial");
|
|
85
|
|
- headFont.setBold(true);
|
|
86
|
|
-
|
|
87
|
|
- // 设置表头单元格样式
|
|
88
|
|
- WriteCellStyle headCellStyle = new WriteCellStyle();
|
|
89
|
|
- headCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
|
|
90
|
|
- headCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
91
|
|
- headCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
|
92
|
|
- headCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
|
|
93
|
|
- headCellStyle.setBorderLeft(BorderStyle.THIN);
|
|
94
|
|
- headCellStyle.setBorderRight(BorderStyle.THIN);
|
|
95
|
|
- headCellStyle.setBorderTop(BorderStyle.THIN);
|
|
96
|
|
- headCellStyle.setBorderBottom(BorderStyle.THIN);
|
|
97
|
|
- headCellStyle.setWriteFont(headFont);
|
|
98
|
|
-
|
|
99
|
|
- // 创建并返回水平单元格样式策略
|
|
100
|
|
- return new HorizontalCellStyleStrategy(headCellStyle, (List<WriteCellStyle>) null);
|
|
101
|
|
- }
|
|
102
|
|
-
|
|
103
|
|
- /**
|
|
104
|
|
- * 获取带有@Excel注解的属性集合
|
|
105
|
|
- *
|
|
106
|
|
- * @param clazz 实体类的Class对象
|
|
107
|
|
- * @return 带有@Excel注解的Field集合
|
|
108
|
|
- */
|
|
109
|
|
- public static Field[] getExcelPropertyAnnotatedFields(Class<?> clazz) {
|
|
110
|
|
- Field[] fields = clazz.getDeclaredFields();
|
|
111
|
|
- List<Field> annotatedFields = new ArrayList<>();
|
|
112
|
|
-
|
|
113
|
|
- for (Field field : fields) {
|
|
114
|
|
- if (field.isAnnotationPresent(ExcelProperty.class)) {
|
|
115
|
|
- annotatedFields.add(field);
|
|
116
|
|
- }
|
|
117
|
|
- }
|
|
118
|
|
- return annotatedFields.toArray(new Field[0]);
|
|
119
|
|
- }
|
|
120
|
|
-
|
|
121
|
|
- /**
|
|
122
|
|
- * 查询类需要导出的列名
|
|
123
|
|
- *
|
|
124
|
|
- * @param clazz
|
|
125
|
|
- * @return
|
|
126
|
|
- */
|
|
127
|
|
- public static Set<String> getIncludeFields(Class<?> clazz) {
|
|
128
|
|
- Field[] excelPropertyAnnotatedFields = getExcelPropertyAnnotatedFields(AppSceneCostStandardDetail.class);
|
|
129
|
|
- Set<String> includeColumnFiledNames = new HashSet<String>();
|
|
130
|
|
- for (int i = 0; i < excelPropertyAnnotatedFields.length; i++) {
|
|
131
|
|
- includeColumnFiledNames.add(excelPropertyAnnotatedFields[i].getName());
|
|
132
|
|
- }
|
|
133
|
|
- return includeColumnFiledNames;
|
|
134
|
|
- }
|
|
135
|
|
-}
|