guohongjin
2024-05-15 5b7639f0bd9e056738ec15100ed0532e965c6cd5
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
package cn.stylefeng.guns.utils;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
 
import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 导出工具类
 *  +----------------------------------------------------------------------
 *  | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
 *  +----------------------------------------------------------------------
 *  | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
 *  +----------------------------------------------------------------------
 *  | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
 *  +----------------------------------------------------------------------
 *  | Author: CRMEB Team <admin@crmeb.com>
 *  +----------------------------------------------------------------------
 */
 
 
public class ExportUtil {
 
 
    //上传类型
    public static final String DOWNLOAD_TYPE_FILE = "downloadf"; // 文件导出下载拦截关键字
    public static final String UPLOAD_TYPE_FILE = "uploadf"; // 文件前端上传后下载关键字
    //    public static final String UPLOAD_ROOT_PATH_CONFIG_KEY = "upload_root_path";//上传地址
    public static final String UPLOAD_MODEL_PATH_EXCEL = "excel";// excel
    /**
     * 导出Excel文件
     * @param fileName  文件名
     * @param title     文件标题
     * @param voList    数据列表
     * @param aliasMap  别名Map(别名需要与数据列表的数据对应)
     * @return 返回给前端的文件名(路径+文件名)
     */
    public static String exportExecl(String fileName, String title, List<?> voList, LinkedHashMap<String, String> aliasMap) throws Exception {
        if (StrUtil.isBlank(fileName)) {
            throw new Exception("文件名不能为空");
        }
        if (StrUtil.isBlank(title)) {
            throw new Exception("标题不能为空");
        }
        if (CollUtil.isEmpty(voList)) {
            throw new Exception("数据列表不能为空");
        }
        if (CollUtil.isEmpty(aliasMap)) {
            throw new Exception("别名map不能为空");
        }
 
        // 文件名部分
        String newFileName = UploadUtil.getWebPath() +  fileName;
        String filePath = UploadUtil.getServerPath();
 
        // 判断是否存在当前目录,不存在则创建
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        fileName = filePath.concat(fileName);
 
        // 通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter(fileName);
//        ExcelWriter writer = ExcelUtil.getWriter("d:/writeMapTest.xlsx");
        CellStyle headCellStyle = writer.getHeadCellStyle();
        Font font = writer.createFont();
        font.setBold(true);
        headCellStyle.setFont(font);
 
        //自定义标题别名
        aliasMap.forEach((key, value) -> writer.addHeaderAlias(key, value));
        // 合并单元格后的标题行,使用默认标题样式
        writer.merge(aliasMap.size() - 1, title);
        writer.merge(aliasMap.size() - 1, StrUtil.format("生成时间:{}", DateUtil.now()));
        //设置宽度自适应
        writer.setColumnWidth(-1, 22);
        // 一次性写出内容,使用默认样式,强制输出标题
        writer.write(voList, true);
        // 关闭writer,释放内存
        writer.close();
 
        return newFileName;
    }
 
    /**
     * 导出Excel文件
     * @param fileName  文件名
     * @param title     文件标题
     * @param voList    数据列表
     * @param aliasMap  别名Map(别名需要与数据列表的数据对应)
     * @return 返回给前端的文件名(路径+文件名)
     */
    public static Map<String,String> exportExeclOss(String fileName, String title, List<?> voList, LinkedHashMap<String, String> aliasMap) throws Exception {
        if (StrUtil.isBlank(fileName)) {
            throw new Exception("文件名不能为空");
        }
        if (StrUtil.isBlank(title)) {
            throw new Exception("标题不能为空");
        }
        if (CollUtil.isEmpty(voList)) {
            throw new Exception("数据列表不能为空");
        }
        if (CollUtil.isEmpty(aliasMap)) {
            throw new Exception("别名map不能为空");
        }
 
        // 文件名部分
        String newFileName = UploadUtil.getWebPath() +  fileName;
        String filePath = UploadUtil.getServerPath();
 
        // 判断是否存在当前目录,不存在则创建
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        fileName = filePath.concat(fileName);
 
        // 通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter(fileName);
//        ExcelWriter writer = ExcelUtil.getWriter("d:/writeMapTest.xlsx");
        CellStyle headCellStyle = writer.getHeadCellStyle();
        Font font = writer.createFont();
        font.setBold(true);
        headCellStyle.setFont(font);
 
        //自定义标题别名
        aliasMap.forEach((key, value) -> writer.addHeaderAlias(key, value));
        // 合并单元格后的标题行,使用默认标题样式
        writer.merge(aliasMap.size() - 1, title);
        writer.merge(aliasMap.size() - 1, StrUtil.format("生成时间:{}", DateUtil.now()));
        //设置宽度自适应
        writer.setColumnWidth(-1, 22);
        // 一次性写出内容,使用默认样式,强制输出标题
        writer.write(voList, true);
        // 关闭writer,释放内存
        writer.close();
        Map<String,String> map = new HashMap<>();
        map.put("fileName",newFileName);
        map.put("filePath",fileName);
        return map;
    }
 
 
    /**
     * 上传部分设置
     */
    public static void setUpload( String modelPath, String type)  {
 
        UploadUtil.setModelPath("guns/"+UPLOAD_TYPE_FILE+"/"+modelPath);
        UploadUtil.setType(type);
    }
 
 
}