1821349743@qq.com
2023-04-17 b92fd92933fce8e97fef05207596217bb746cd4d
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
package com.product.module.data.service;
 
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.product.core.config.CoreConst;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.entity.RequestParameterEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.module.data.config.CmnCode;
import com.product.module.data.config.CmnConst;
import com.product.module.data.utli.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
 
/**
 * @Author cheng
 * @Description 数据导出service
 * @Date 2021/6/10 15:02
 * @Version 1.0
 */
@Service
public class SystemDataExportService extends AbstractBaseService {
    @Autowired
    SystemApiToMethods systemApiToMethods;
 
    @Override
    public BaseDao getBaseDao() {
        return super.getBaseDao();
    }
 
    public void reportDataExport(FieldSetEntity fse) throws BaseException {
        //报表uuid
        String uuid = fse.getUUID();
        //是否当前页
        boolean exportCurrentPage = fse.getBoolean("exportCurrentPage");
        //当前页号
        int pageIndex = fse.getInteger(CmnConst.CPAGE);
 
    }
 
    /**
     * 数据报表导出
     *
     * @param reqp
     * @return 返回Base64 EXCEL.XLSX
     * @throws BaseException
     * @throws IOException
     */
    public String reportDataExport(RequestParameterEntity reqp) throws BaseException {
 
        FieldSetEntity formData = reqp.getFormData();
        //总列数
        Integer totalColumn = formData.getInteger("totalColumn");
        //总行数
        Integer totalRows = formData.getInteger("totalRows");
        //头部区域行数
        Integer headCount = formData.getInteger("headCount");
        Map<String, File> files = reqp.getFiles();
        //固定文件名
        File portExcel = files.get("portExcel.xlsx");
        try (InputStream inputStream = new FileInputStream(portExcel); XSSFWorkbook wb = new XSSFWorkbook(inputStream)) {
            XSSFSheet sheet = wb.getSheetAt(0);
            //获取头部样式
            XSSFCellStyle headStyle = getReportStyle(wb, IndexedColors.BLACK.getIndex(), true, (short) 15, new java.awt.Color(191, 191, 191));
            //获取数据区样式
            XSSFCellStyle dataStyle = getReportStyle(wb, IndexedColors.BLACK.getIndex(), false, (short) 13, new java.awt.Color(255, 255, 255));
            //所有同列最宽
            Map<Integer, Integer> m = new HashMap<>();
            for (int i = 0; i < totalRows; i++) {
                XSSFRow row = sheet.getRow(i);
                XSSFCellStyle style = null;
                if (i < headCount) {
                    //头部区域样式
                    style = headStyle;
                } else {
                    //数据区样式
                    style = dataStyle;
                }
                //遍历每一个单元格
                for (int j = 0; j < totalColumn; j++) {
                    //获取单元格对象
                    XSSFCell cell = row.getCell(j);
                    if (cell == null) {
                        //填充空的单元格
                        cell = row.createCell(j, CellType.STRING);
                    }
                    //设置单元格样式
                    cell.setCellStyle(style);
                    //获取单元格值
                    String val = cell.getStringCellValue();
                    if (val != null) {
                        //根据字节计算大致宽度
                        int i2 = val.getBytes().length * 256;
                        //宽度不能超过 256*256
                        if (i2 > 256 * 256) {
                            i2 = 256 * 255;
                        }
                        if (m.get(j) == null) {
                            m.put(j, i2);
                        } else {
                            //当前行的列是否比其他行的列宽
                            Integer width = m.get(j);
                            if (width < i2) {
                                m.put(j, i2);
                            }
                        }
                    }
 
                }
            }
            //设置每列的宽度
            for (Map.Entry<Integer, Integer> v : m.entrySet()) {
                sheet.setColumnWidth(v.getKey(), v.getValue());
            }
            //将workbook转换为字节流
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            //将excel输出到字节流
            wb.write(byteArrayOutputStream);
            //将字节流转为字节
            byte[] bytes = byteArrayOutputStream.toByteArray();
            //将字节转换为Base64
            String encode = Base64.getEncoder().encodeToString(bytes);
            //删除传入的文件
            if (portExcel != null && portExcel.exists()) {
                portExcel.delete();
            }
            //返回Base64字符串 拼接XLSX文件格式前缀
            return "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64," + encode;
        } catch (Exception e) {
            throw new BaseException(CmnCode.EXPORT_REPORT_DATA_FIAL.getValue(), CmnCode.EXPORT_REPORT_DATA_FIAL.getValue() + e.getMessage());
        }
    }
 
    /**
     * 单元格样式
     *
     * @param workbook
     * @param color    字体颜色
     * @param bold     是否加粗
     * @param fontSize 字体大小
     * @param bgc      背景色
     * @return
     */
    public XSSFCellStyle getReportStyle(XSSFWorkbook workbook, short color, boolean bold, short fontSize, java.awt.Color bgc) {
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        //左右居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        //上下居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 下边框
        cellStyle.setBorderBottom(BorderStyle.THIN);
        // 左边框
        cellStyle.setBorderLeft(BorderStyle.THIN);
        // 上边框
        cellStyle.setBorderTop(BorderStyle.THIN);
        // 右边框
        cellStyle.setBorderRight(BorderStyle.THIN);
        //设置背景色
//        cellStyle.setFillForegroundColor(new XSSFColor());
        //填充模式
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        XSSFFont font = workbook.createFont();
        //字体颜色
        font.setColor(color);
        //加粗
        font.setBold(bold);
        //字体大小
        font.setFontHeightInPoints(fontSize);
        //字体样式
        font.setFontName("微软雅黑");
        //自动换行
        cellStyle.setWrapText(true);
        cellStyle.setFont(font);
        return cellStyle;
    }
 
 
    /**
     * 通用列表导出
     *
     * @param fse
     * @throws BaseException
     */
    public void generalListDataExport(FieldSetEntity fse) throws BaseException {
        //导出文件名称
        String file_name = fse.getString(CmnConst.FILE_NAME);
        //数据接口
        String upload_api_url = fse.getString(CmnConst.UPLOAD_API_URL);
        //数据接口所需参数
        FieldSetEntity export_param = fse.getSubDataTable(CmnConst.EXPORT_PARAM).getFieldSetEntity(0);
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        RequestParameterEntity requestParameterEntity = (RequestParameterEntity) request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
        FieldSetEntity fs = new FieldSetEntity();
        fs.setTableName(fse.getString(CmnConst.TABLE_NAME));
        //开始页
        int start_cpage = export_param.getInteger(CmnConst.START_CPAGE);
        //结束页
        int end_cpage = export_param.getInteger(CmnConst.END_CPAGE);
        int pagesize = export_param.getInteger(CmnConst.PAGESIZE);
        export_param.setValue(CmnConst.CPAGE, ((start_cpage) * pagesize) / pagesize);
        export_param.setValue(CmnConst.PAGESIZE, pagesize * ((end_cpage - start_cpage) + 1));
        export_param.remove(CmnConst.START_CPAGE);
        export_param.remove(CmnConst.END_CPAGE);
        Map<Object, Object> values = export_param.getValues();
        for (Map.Entry<Object, Object> v : values.entrySet()) {
            fs.setValue(v.getKey().toString(), v.getValue());
        }
        requestParameterEntity.setFormData(fs);
        String result = (String) systemApiToMethods.run(upload_api_url, 1);
 
        if (JSON.isValidObject(result)) {
            JSONObject resultJson = JSON.parseObject(result);
            if (200 == resultJson.getInteger("code")) {
                //获取数据成功
                Object data = resultJson.get("data");
                if (data instanceof JSONObject) {
                } else if (data instanceof JSONArray) {
                    DataTableEntity export_field = export_param.getSubDataTable("export_field");
                    try {
                        writeExcel((JSONArray) data, export_field, file_name);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } else {
                throw new BaseException(resultJson.getString("code"), resultJson.getString("msg"));
            }
        } else {
            System.out.println("未知的数据类型");
        }
    }
 
    /**
     * 输出excel
     *
     * @param data
     * @param fieldInfo
     * @param main_title
     * @throws IOException
     */
    public void writeExcel(JSONArray data, DataTableEntity fieldInfo, String main_title) throws IOException {
        //标题
        List<List<String>> headTitles = Lists.newArrayList();
        List<String> fields = Lists.newArrayList();
        //导出的数据集
        List<String> titles = Lists.newArrayList();
        List<String> titleTemplate = Lists.newArrayList();
        titleTemplate.add(main_title);
        for (int i = 0; i < fieldInfo.getRows(); i++) {
            titles.add(fieldInfo.getString(i, CmnConst.FIELD_DESC));
            fields.add(fieldInfo.getString(i, CmnConst.FIELD_NAME));
        }
        List<List<Object>> lists = contentData(data, fields);
        titles.forEach(title -> {
            List<String> secondTitle = Lists.newArrayList(titleTemplate);
            secondTitle.add(title);
            headTitles.add(secondTitle);
        });
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        writeExcel(headTitles, lists, main_title, response);
    }
 
    /**
     * 输出excel
     *
     * @param excelHeaders excel表头
     * @param dataRows     数据行
     * @param sheetName    sheet名称
     * @throws IOException
     */
    public void writeExcel(List<List<String>> excelHeaders, List dataRows, String sheetName, HttpServletResponse response) throws IOException {
 
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        // 这里需要设置不关闭流
        try (ServletOutputStream outputStream = response.getOutputStream();) {
            EasyExcel.write(outputStream).
                    registerWriteHandler(new Custemhandler()).head(excelHeaders).sheet(sheetName).doWrite(dataRows);
        }
    }
 
 
    /**
     * 组装数据
     *
     * @param dataArray
     * @param fields
     * @return
     */
    private static List<List<Object>> contentData(JSONArray dataArray, List<String> fields) {
        List<List<Object>> contentList = Lists.newArrayList();
        dataArray.forEach(data -> {
            JSONObject dataJson = (JSONObject) data;
            List<Object> content = Lists.newArrayList();
            fields.forEach(field -> {
                content.add(dataJson.get(field));
            });
            contentList.add(content);
        });
        return contentList;
    }
 
 
}