forked from kidgrow-microservices-platform

luliqiang
2020-12-31 6fb14149d62199cfcc0448c82eb2f51f9c5181de
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package com.kidgrow.common.utils;
 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.google.common.collect.Lists;
import com.kidgrow.common.utils.excelModel.ExportField;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.*;
 
/**
 * @author : heyanfeng
 * create at:  2019-05-27  19:11
 * @description: excel
 */
@Slf4j
public class ExcelUtils {
    public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response){
        ExportParams exportParams = new ExportParams(title, sheetName);
        exportParams.setCreateHeadRows(isCreateHeader);
        defaultExport(list, pojoClass, fileName, response, exportParams);
 
    }
    public static void  exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName, HttpServletResponse response){
        defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
    }
    public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){
        defaultExport(list, fileName, response);
    }
    public static void exportExcel(String fileName,ExportParams exportParams, List<ExcelExportEntity> entities,List<Map<String, Object>> list,HttpServletResponse response){
        defaultExport(fileName,exportParams,entities,list,response);
    }
 
    private  static void defaultExport(String fileName,ExportParams exportParams, List<ExcelExportEntity> entities,List<Map<String, Object>> list,HttpServletResponse response ){
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, entities, list);
        if (workbook != null){
        downLoadExcel(fileName, response, workbook);}
    }
 
    private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
        if (workbook != null) {
            downLoadExcel(fileName, response, workbook);
        }
    }
 
    private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            log.error("excel异常:{}",e.getMessage());
        }
    }
    private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
        Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
        if (workbook != null){
        downLoadExcel(fileName, response, workbook);}
    }
    public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass){
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
            File file = new File(filePath);
            list = ExcelImportUtil.importExcel(file, pojoClass, params);
        }catch (NoSuchElementException e){
            throw new IllegalArgumentException("模板不能为空");
        } catch (Exception e) {
            log.error("excel异常:{}",e.getMessage());
        }
        return list;
    }
 
    public static <T> List<T> importExcel(InputStream inputStream, Integer titleRows, Integer headerRows, Class<T> pojoClass){
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(inputStream, pojoClass, params);
        }catch (NoSuchElementException e){
            throw new IllegalArgumentException("模板不能为空");
        } catch (Exception e) {
            log.error("excel异常:{}",e.getMessage());
        }
        return list;
    }
    public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){
        if (file == null){
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
        }catch (NoSuchElementException e){
            throw new IllegalArgumentException("excel文件不能为空");
        } catch (Exception e) {
            log.error("excel异常:{}",e.getMessage());
        }
        return list;
    }
 
 
    /**
     * description: 动态生成excel 列
     * create by heyanfeng at 2019-05-29 18:02
     * @param ef
     * @param entities
     */
    public static void dynamicNewAddExcel(List<ExportField> ef, List<ExcelExportEntity> entities) {
        //单元格的excel 表头
        ef.forEach(item -> {
            //需要合并单元格的表头
            List<ExportField> children = item.getChildren();
            if(!CollectionUtils.isEmpty(children)){
                ExcelExportEntity parent = new ExcelExportEntity(item.getFiledChineseName(), item.getEntityAttrName());
                List<ExcelExportEntity> entitiesChildren = Lists.newArrayList();
                children.forEach(e -> {
                    entitiesChildren.add(new ExcelExportEntity(e.getFiledChineseName(),e.getEntityAttrName(),30));
                });
                parent.setNeedMerge(true);
                parent.setList(entitiesChildren);
                entities.add(parent);
            }else{
                entities.add(new ExcelExportEntity(item.getFiledChineseName(),item.getEntityAttrName(),30));
            }
        });
    }
 
 
    /**
     * description: 根据属性名称 获取属性的值
     * create by heyanfeng at 2019-05-29 19:26
     * @return Object
     * @param fieldName
     * @param o
     */
     public static  Object getFieldValueByName(String fieldName,Object o){
        try{
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[] {});
            Object value = method.invoke(o, new Object[]{});
            return  value;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }
 
    /**
     * description: 组装excel 数据
     * create by heyanfeng at 2019-05-29 20:30
     * @return  List<Map<String, Object>>
     * @param ef
     * @param statisData
     */
    public static List<Map<String, Object>> dynamicListDataByKey(List<ExportField> ef, List<?> statisData) {
        //最终的数据
        List<Map<String, Object>> datas = new ArrayList<>();
        Map map;
        for (Object t : statisData) {
            map = new HashMap();
            for (int j = 0; j < ef.size(); j++) {
                List<ExportField> children = ef.get(j).getChildren();
                if(!CollectionUtils.isEmpty(children)){
                   //遍历需要合并单元格的子列
                    traversechildren(map, t, children,ef.get(j).getEntityAttrName());
                }else if(StringUtils.isNotBlank(ef.get(j).getEntityAttrName())){
                    map.put(ef.get(j).getEntityAttrName(),getFieldValueByName(ef.get(j).getEntityAttrName(),t));
                }
            }
            datas.add(map);
        }
        return datas;
    }
 
    /**
    * description: 遍历需要合并单元格的子列
    * create by heyanfeng at 2019-05-31 14:19
    */
    private static void traversechildren(Map map, Object t, List<ExportField> children,String entityAttrName) {
        ArrayList<Map<String,Object>> childrenMaps = Lists.newArrayList();
        Map<String,Object> childrenMap= new HashMap();
        for (int k = 0; k < children.size(); k++) {
            if(StringUtils.isNotBlank(children.get(k).getEntityAttrName())){
                childrenMap.put(children.get(k).getEntityAttrName(),getFieldValueByName(children.get(k).getEntityAttrName(),t));
            }
        }
        childrenMaps.add(childrenMap);
        map.put(entityAttrName,childrenMaps);
    }
 
 
    /**
    * description: 组装sql 查询条件
    * create by heyanfeng at 2019-05-30 10:09
    */
//    public static String getExcelSql(List<ExportField> exportField) {
//        String fileds;
//        StringBuilder stringBuilder = new StringBuilder();
//        exportField.forEach(item -> {
//            //如果存在需要合并表头的列 则遍历
//            List<ExportField> children = item.getChildren();
//            if(!CollectionUtils.isEmpty(children)){
//                children.forEach(e -> {
//                    if(StringUtils.isNotBlank(e.getFiled())){
//                        stringBuilder.append(e.getFiled() + GlobalConstant.PIC_SPLIT);
//                    }
//                });
//            }else if(StringUtils.isNotBlank(item.getFiled())){
//                stringBuilder.append(item.getFiled() + GlobalConstant.PIC_SPLIT);
//            }
//        });
//        stringBuilder.deleteCharAt(stringBuilder.lastIndexOf(GlobalConstant.PIC_SPLIT));
//        fileds = stringBuilder.toString();
//        return fileds;
//    }
 
    /**
     * description: 组装sql 查询条件
     * create by heyanfeng at 2019-05-30 10:09
     */
//    public static String getGroupBySql(List<ExportField> exportField) {
//        String fileds;
//        //是否加入 group by
//        Boolean[] flag = {false};
//        StringBuilder stringBuilder = new StringBuilder();
//        exportField.forEach(item -> {
//            if(item.getGroupBy() == null && StringUtils.isNotBlank(item.getFiled())){
//                stringBuilder.append(item.getFiled() + GlobalConstant.PIC_SPLIT);
//            }else if(item.getGroupBy() != null  && item.getGroupBy() == YESNOEnum.YES.getValue()){
//                flag[0] = true;
//            }
//        });
//        stringBuilder.deleteCharAt(stringBuilder.lastIndexOf(GlobalConstant.PIC_SPLIT));
//        fileds =  stringBuilder.toString();
//        if (flag[0]){
//            fileds =   " group by " + fileds;
//            return  fileds;
//        }
//        return "";
//    }
 
 
    /**
    * description: 检查导出字段是否为空
    * create by heyanfeng at 2019-05-30 10:10
    */
    public  static void checkExportField( List<ExportField> exportField) {
        if(CollectionUtils.isEmpty(exportField)){
            throw new IllegalArgumentException("导出的字段不能为空");
        }
    }
 
    /**
    * description: 动态导出生成excel
    * create by heyanfeng at 2019-05-30 10:37
    */
    public static void DynamicExcel(String fileName, String title,String sheet,List<ExportField> exportField, HttpServletResponse response, List<?> list) {
        //生成动态列
        List<ExcelExportEntity> entities = Lists.newArrayList();
        ExcelUtils.dynamicNewAddExcel(exportField, entities);
        //组装数据entities
        List<Map<String, Object>> maps = ExcelUtils.dynamicListDataByKey(exportField, list);
        ExcelUtils.exportExcel(fileName,new ExportParams(title, sheet),entities,maps,response);
    }
}