jinlin
2023-11-27 5d8c028ff37838e0628d5852e41b19efc80e4be1
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
package com.zt.common.utils;
 
import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.TableStyle;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
 
import static java.lang.Thread.currentThread;
import static java.lang.Thread.sleep;
 
/**
 * excel工具类
 *
 * @author Mark sunlightcs@gmail.com
 */
public class ExcelUtils {
 
    public static void export(HttpServletResponse response, String fileName, List list, Class<?> pojoClass)
            throws IOException {
        if (StringUtils.isBlank(fileName)) {
            // 当前日期
            fileName = DateUtil.now();
        } else {
            fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
            fileName = fileName + "_" + DateUtil.now();
        }
 
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), pojoClass).sheet("sheet1").doWrite(list);
    }
 
    public static void export(HttpServletResponse response, String fileName, String[] sheets, List[] lists)
            throws IOException {
        if (StringUtils.isBlank(fileName)) {
            // 当前日期
            fileName = DateUtil.now();
        } else {
            fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
            fileName = fileName + "_" + DateUtil.now();
        }
 
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build(); ;
        try {
            for (int i = 0; i < sheets.length; i++) {
                String sheet = sheets[i];
                List datas = lists[i];
                if (datas != null && datas.size() > 0) {
                    WriteSheet writeSheet = EasyExcel.writerSheet(i, sheet).head(datas.get(0).getClass()).build();
                    excelWriter.write(datas, writeSheet);
                }
            }
        } finally {
            // 千万别忘记finish 会帮忙关闭流
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }
 
    /**
     * Excel导出,先sourceList转换成List<targetClass>,再导出
     *
     * @param response
     *            response
     * @param fileName
     *            文件名
     * @param sourceList
     *            原数据List
     * @param targetClass
     *            目标对象Class
     */
    public static void exportExcelToTarget(HttpServletResponse response, String fileName, Collection<?> sourceList,
            Class<?> targetClass) throws Exception {
        List targetList = new ArrayList<>(sourceList.size());
        for (Object source : sourceList) {
            Object target = targetClass.newInstance();
            BeanUtils.copyProperties(source, target);
            targetList.add(target);
        }
 
        export(response, fileName, targetList, targetClass);
    }
 
    public static List<List<String>> parseClipboard(String clipboard) {
        // 1. 将双引号替换成特殊字符串
        String text = clipboard.replace("\"\"", "goma---Hirake");
        // 2. 将引号之间的换行替换成特殊字符串
        StringBuilder sb = new StringBuilder();
        boolean inQuote = false;
        for (int i=0; i< text.length(); i++) {
            char chr = text.charAt(i);
            if (chr == '"') {
                inQuote = !inQuote;
            }
            if (inQuote) {
                if (chr == '\n') {
                    sb.append("goma+++Hirake");
                } else {
                    sb.append(chr);
                }
            } else {
                sb.append(chr);
            }
        }
        // 3. 切分行和列
        List<List<String>> lists = new ArrayList<>();
        String[] rows = sb.toString().split("\n");
        for (int i=0; i<rows.length; i++) {
            List<String> list = new ArrayList<>();
            String[] columns = rows[i].split("\t");
            for (int j=0; j<columns.length; j++) {
                String cell = columns[j].replace("\"", "").
                        replace("goma---Hirake", "\"").
                        replace("goma+++Hirake", "\n");
                list.add(cell);
            }
            lists.add(list);
        }
 
        return lists;
    }
 
    public static void main(String[] args) {
        String clipboard = "\"新增意见反馈功能:\n" +
                "1)页面标题区加意见反馈按钮,放在帮助旁\"\"\"\"\n" +
                "2)所有用户都可以看,都可以提意见反馈\n" +
                "3)填写如下内容(都是必填):意见、反馈人姓名、单位、联系方式(电话)、是否采纳(采纳/解释)、闭环情况(是/否)、备注(解释内容)。由admin用户填写是否采纳,如果是解释,则在备注中填写解释内容。\"\t2023/6/9\n" +
                "工程勘验、施工明细和修理工程单的列表:鼠标所在的行要明显些,框线要明显些,字体调大,做成18px。(投屏时看不清晰)\t2023/6/9\n" +
                "优化列表显示速度\t2023/6/9\n";
        List<List<String>> res = parseClipboard(clipboard);
        for (List<String> list : res) {
            for (String s : list) {
                System.out.println(s);
            }
        }
    }
}