zhuguifei
2025-04-28 442928123f63ee497d766f9a7a14f0a6ee067e25
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package org.jeecg.modules.excel;
 
import com.google.common.base.Joiner;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.feign.SystemClient;
import org.jeecg.modules.feign.model.SysUser;
import org.jeecg.modules.project.entity.ProProject;
import org.jeecg.modules.project.entity.ProProjectLink;
import org.jeecg.modules.project.entity.ProSummary;
import org.jeecg.modules.project.vo.ProjectVo;
import org.jxls.command.CellDataUpdater;
import org.jxls.common.CellData;
import org.jxls.common.CellRef;
import org.jxls.common.Context;
import org.jxls.transform.poi.PoiCellData;
import org.jxls.transform.poi.PoiTransformer;
import org.jxls.transform.poi.PoiUtil;
import org.jxls.util.JxlsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
 
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
 
@Component
public class ExcelUtils {
    @Autowired
    private SystemClient systemClient;
    @Value(value = "${jeecg.path.upload}")
    private String uploadpath;
 
    //读取的excel 转为项目bean
    public ProProject CoverProProject(List<Map<Integer, String>> listMap) {
        Map<String, String> usersMap = new HashMap<>();
        ProProject project = new ProProject();
        ProSummary summary = new ProSummary();
 
        Map<Integer, String> map0 = listMap.get(0);
        project.setXmmc(map0.get(2));//项目名称
 
        String xmfzr = map0.get(4);
 
        parseUsers(xmfzr, project, usersMap, null, 1);
 
 
        project.setBgrq(getDate(map0.get(6)));// 报告日期
        project.setXmbh(map0.get(8));//项目编号
 
        Map<Integer, String> map1 = listMap.get(1);
        project.setXmbj(map1.get(2));//项目背景
 
        Map<Integer, String> map2 = listMap.get(2);
        project.setXmmb(map2.get(2));//项目目标
 
        Map<Integer, String> map3 = listMap.get(3);
        summary.setJqmb(map3.get(2));//近期目标
 
        Map<Integer, String> map4 = listMap.get(4);
 
        project.setKsrq(getDate(map4.get(2)));  // 开始日期
 
 
        Map<Integer, String> map5 = listMap.get(5);
        project.setJsrq(getDate(map5.get(2)));// 结束日期
 
        Map<Integer, String> map6 = listMap.get(6);
        summary.setBgsx(map6.get(2));// 变更事项
        Map<Integer, String> map7 = listMap.get(7);
        //项目集code
        project.setGroupCode(map7.get(9));
 
        Map<Integer, String> map8 = listMap.get(8);//概述
        summary.setGs(map8.get(2));
        summary.setJdsfzc(map8.get(4));
        summary.setSfxyzc(map8.get(5));
        summary.setSfyxdd(map8.get(6));
        summary.setFxyc(map8.get(7));
 
        Map<Integer, String> map9 = listMap.get(9);//预测
        summary.setYc(map9.get(2));
 
 
 
 
 
 
       /* Map<Integer, String> map4 = listMap.get(4);
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(map4.get(1));
        project.setSysj(Integer.parseInt(m.replaceAll("").trim())); //剩余时间*/
 
/*        Map<Integer, String> map5 = listMap.get(5);
        project.setBzgz(map5.get(1));//本周工作
 
        Map<Integer, String> map6 = listMap.get(6);
        project.setXzgz(map6.get(1));//下周工作*/
 
        //从excel第9行开始读'项目环节'
        List<ProProjectLink> linkList = new ArrayList<>();
        for (int i = 11; i < listMap.size(); i++) {
            Map<Integer, String> map = listMap.get(i);
            ProProjectLink link = new ProProjectLink();
            if (map.get(0) == null && map.get(1) == null && map.get(2) == null && map.get(3) == null && map.get(4) == null) {
                //如果多列都没数据,说明下方为空白
                break;
            }
            link.setSxh(map.get(1));//原始序号
            if (map.containsKey(2))
                link.setXmhj(map.get(2)); //项目环节
            if (map.containsKey(3))
                link.setQssj(getDate(map.get(3))); //起始时间
            if (map.containsKey(4))
                link.setYjwcsj(getDate(map.get(4))); //预计完成时间
            if (map.containsKey(5))
 
                link.setCxsj(getDay(map.get(5)));//持续时间
            if (map.containsKey(6))
                link.setSjwcsj(getDate(map.get(6)));//实际完成时间
            if (map.containsKey(7))
                if (!StringUtils.isEmpty(map.get(7)) && map.get(7).equals("√"))
                    link.setWcjd(1);
                else
                    link.setWcjd(0);
            //TODO 资源名称为用户真实姓名  存入系统后无法对应
            if (map.containsKey(8))
                parseUsers(map.get(8), null, usersMap, link, 2);
 
            //根据序号判断菜单层级
            String sxh = link.getSxh();
            //没有序号则不导入
            if (StringUtils.isEmpty(sxh)) continue;
            char[] chars = sxh.toCharArray();
            int dotCount = 0;
            char dot = '.'; //根据有几个点判断是几级菜单
            for (int j = 0; j < chars.length; j++) {
                if (chars[j] == dot) dotCount++;
            }
 
            //判断等级
            //1级菜单
            if (dotCount == 0) {
                link.setLevel(1);
                link.setLevel1(getDay(link.getSxh()));
            }
            //2级菜单
            else if (dotCount == 1) {
                String str1 = link.getSxh().substring(0, link.getSxh().indexOf("."));
                String str2 = link.getSxh().substring(link.getSxh().indexOf(".") + 1);
                link.setLevel(2);
                link.setLevel1(getDay(str1));
                link.setLevel2(getDay(str2));
            }
            //设置3级目录序号
            else if (dotCount == 2) {
                String str1 = link.getSxh().substring(0, link.getSxh().indexOf("."));
                String str2 = link.getSxh().substring(link.getSxh().indexOf(".") + 1, link.getSxh().lastIndexOf("."));
                String str3 = link.getSxh().substring(link.getSxh().lastIndexOf(".") + 1);
                link.setLevel(3);
                link.setLevel1(getDay(str1));
                link.setLevel2(getDay(str2));
                link.setLevel3(getDay(str3));
            }
            //4级菜单
            else if (dotCount == 3) {
                String substring1 = sxh.substring(0, sxh.indexOf("."));
                sxh = sxh.substring(sxh.indexOf(".") + 1);
                String substring2 = sxh.substring(0, sxh.indexOf("."));
                sxh = sxh.substring(sxh.indexOf(".") + 1);
                String substring3 = sxh.substring(0, sxh.indexOf("."));
                sxh = sxh.substring(sxh.indexOf(".") + 1);
                String substring4 = sxh;
                link.setLevel(4);
                link.setLevel1(getDay(substring1));
                link.setLevel2(getDay(substring2));
                link.setLevel3(getDay(substring3));
                link.setLevel4(getDay(substring4));
            }
            //5级菜单
            else if (dotCount == 4) {
                String substring1 = sxh.substring(0, sxh.indexOf("."));
                sxh = sxh.substring(sxh.indexOf(".") + 1);
                String substring2 = sxh.substring(0, sxh.indexOf("."));
                sxh = sxh.substring(sxh.indexOf(".") + 1);
                String substring3 = sxh.substring(0, sxh.indexOf("."));
                sxh = sxh.substring(sxh.indexOf(".") + 1);
                String substring4 = sxh.substring(0, sxh.indexOf("."));
                sxh = sxh.substring(sxh.indexOf(".") + 1);
                String substring5 = sxh;
                link.setLevel(5);
                link.setLevel1(getDay(substring1));
                link.setLevel2(getDay(substring2));
                link.setLevel3(getDay(substring3));
                link.setLevel4(getDay(substring4));
                link.setLevel5(getDay(substring5));
            }
            //4级菜单
            else if (dotCount == 5) {
                String substring1 = sxh.substring(0, sxh.indexOf("."));
                sxh = sxh.substring(sxh.indexOf(".") + 1);
                String substring2 = sxh.substring(0, sxh.indexOf("."));
                sxh = sxh.substring(sxh.indexOf(".") + 1);
                String substring3 = sxh.substring(0, sxh.indexOf("."));
                sxh = sxh.substring(sxh.indexOf(".") + 1);
                String substring4 = sxh.substring(0, sxh.indexOf("."));
                sxh = sxh.substring(sxh.indexOf(".") + 1);
                String substring5 = sxh.substring(0, sxh.indexOf("."));
                sxh = sxh.substring(sxh.indexOf(".") + 1);
                String substring6 = sxh;
                link.setLevel(6);
                link.setLevel1(getDay(substring1));
                link.setLevel2(getDay(substring2));
                link.setLevel3(getDay(substring3));
                link.setLevel4(getDay(substring4));
                link.setLevel5(getDay(substring5));
                link.setLevel6(getDay(substring6));
            }
 
            linkList.add(link);
        }
 
 
        project.setLinkList(linkList);
        project.setSummary(summary);
 
        return project;
    }
 
    /**
     * @param userStr  真实姓名
     * @param project  项目
     * @param usersMap 缓存用户真实姓名查出来的username
     * @param type     1-项目负责人 2-项目成员
     */
    private void parseUsers(String userStr, ProProject project, Map<String, String> usersMap, ProProjectLink link, int type) {
        List<String> userList = new ArrayList<>();
        if (!StringUtils.isEmpty(userStr)) {
            String[] split = userStr.split(",");
            if (split != null && split.length > 0) {
                List<String> list = Arrays.asList(split);
                list = list.stream().filter(x -> x.length() > 1).collect(Collectors.toList());
                for (int i = 0; i < list.size(); i++) {
                    String item = list.get(i);
                    if (usersMap.containsKey(item)) {
                        userList.add(usersMap.get(item));
                    } else {
                        Result<SysUser> user = systemClient.queryByName(item);
                        if (user != null && user.getResult() != null && user.getResult().getUsername() != null) {
                            userList.add(user.getResult().getUsername());
                            usersMap.put(item, user.getResult().getUsername());
                        }
                    }
 
                }
                if (userList.size() > 0) {
                    String join = Joiner.on(",").join(userList);
                    if (type == 1) {
                        project.setXmfzr(join);
                    } else {
                        link.setZymc(join);
                    }
 
                }
 
 
            }
 
        }
    }
 
    public static Integer getDay(String str) {
        if (!StringUtils.isEmpty(str)) {
            try {
                String regEx = "[^0-9]";
                Pattern p = Pattern.compile(regEx);
                Matcher m = p.matcher(str);
                String res = m.replaceAll("").trim();
                return Integer.parseInt(res);
            } catch (Exception e) {
                e.printStackTrace();
            }
 
        }
        return null;
    }
 
    public static Date getDate(String str) {
        if (!StringUtils.isEmpty(str)) {
            try {
                if (str.contains("年")) {
 
                    return DateUtils.str2Date(str, new SimpleDateFormat("yyyy年MM月dd日"));
                } else {
                    return DateUtils.str2Date(str, new SimpleDateFormat("yyyy/MM/dd"));
                }
 
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
 
 
    public String exportProject(List<Map<String, Object>> pmo, List<String> sheetNames,List<Map<String, Object>> lcb, List<String> lcbSheetNames,List<Map<String, Object>> ylb, List<String> ylbSheetNames,List<ProjectVo> dtProList, List<String> dtSheetNames) throws Exception {
//        String start = DateUtils.weekToDayStartStr(DateUtils.getCurYear(), DateUtils.getCurWeek(), "yyyy-MM-dd");
//        String weekInMonth = DateUtils.getWeekInMonth(DateUtils.str2Date(start, DateUtils.date_sdf.get()));
        String end = DateUtils.weekToFriDayEndStr(DateUtils.getCurYear(),DateUtils.getCurWeek(), "yyyy-MM-dd");
        String weekInMonth = DateUtils.getWeekInMonth(end,2);
        String templateFileName = uploadpath + File.separator + "exportTemplate" + File.separator + "pro" + File.separator + "tp_project.xlsx";
        String filePath = "export" + File.separator + "项目工作看板-" + weekInMonth + DateUtils.date2Str(new Date(),DateUtils.timeFormat.get()) + ".xlsx";
 
        String fileName = uploadpath + File.separator + filePath;
        File templateFile = new File(templateFileName);
 
        templateFile.deleteOnExit();
        InputStream is = new FileInputStream(templateFile);
        File file = new File(fileName);
        // 流
        OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
 
 
        Context context = PoiTransformer.createInitialContext();
        context.putVar("departments", pmo);
        context.putVar("sheetNames", sheetNames);
 
        context.putVar("lcbProList", lcb);
        context.putVar("lcbSheetNames", lcbSheetNames);
 
        context.putVar("ylbProList", ylb);
        context.putVar("ylbSheetNames", ylbSheetNames);
 
        context.putVar("dtProList", dtProList);
        context.putVar("dtSheetNames", dtSheetNames);
 
 
        JxlsHelper
                .getInstance()
                .setUseFastFormulaProcessor(false)
                .setDeleteTemplateSheet(true)
                .processTemplate(is, os, context);
 
      return filePath;
        // context.putVar("myCellUpdater", new MyCellUpdater());
          /*  // 获取文件配置
            JxlsHelper helper = JxlsHelper.getInstance();
            // 绑定 输入输出
            PoiTransformer transformer = (PoiTransformer) helper.createTransformer(is, os);
            // 获取 工作簿
            Workbook workbook = transformer.getWorkbook();
            // 获取表
            Sheet sheet = workbook.getSheetAt(0);
            // 强制 表格执行公式
            sheet.setForceFormulaRecalculation(true);
            // 放入数据
            helper.processTemplate(context, transformer);*/
 
 
    }
 
    public static class MyCellUpdater implements CellDataUpdater {
 
        @Override
        public void updateCellData(CellData cellData, CellRef targetCell, Context context) {
 
 
            PoiCellData poiCellData = (PoiCellData) cellData;
 
            poiCellData.getCellStyle().setFillForegroundColor(IndexedColors.YELLOW.getIndex());
            System.err.println(poiCellData);
        }
    }
}