|
|
1
|
+package com.lframework.xingyun.sc.excel.ledger;
|
|
|
2
|
+
|
|
|
3
|
+
|
|
|
4
|
+import cn.hutool.core.io.resource.ClassPathResource;
|
|
|
5
|
+import com.lframework.starter.common.exceptions.impl.DefaultClientException;
|
|
|
6
|
+import com.lframework.starter.mq.core.components.export.ExportHandler;
|
|
|
7
|
+import com.lframework.starter.web.core.utils.ApplicationUtil;
|
|
|
8
|
+import com.lframework.starter.web.core.utils.JsonUtil;
|
|
|
9
|
+import com.lframework.starter.web.inner.entity.SysDataDicItem;
|
|
|
10
|
+import com.lframework.starter.web.inner.service.system.SysDataDicItemService;
|
|
|
11
|
+import com.lframework.xingyun.sc.bo.ledger.receipt.ReceiptLedgerReportDetail;
|
|
|
12
|
+import com.lframework.xingyun.sc.utils.ExcelUtil;
|
|
|
13
|
+import com.lframework.xingyun.sc.vo.ledger.receipt.ReceiptLedgerReportVo;
|
|
|
14
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
15
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
16
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
17
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
18
|
+import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
19
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
20
|
+import org.springframework.stereotype.Component;
|
|
|
21
|
+
|
|
|
22
|
+import java.io.File;
|
|
|
23
|
+import java.io.FileOutputStream;
|
|
|
24
|
+import java.io.InputStream;
|
|
|
25
|
+import java.util.HashMap;
|
|
|
26
|
+import java.util.List;
|
|
|
27
|
+import java.util.Map;
|
|
|
28
|
+
|
|
|
29
|
+
|
|
|
30
|
+@Slf4j
|
|
|
31
|
+@Component
|
|
|
32
|
+public class LedgerReportExportHandler implements ExportHandler {
|
|
|
33
|
+
|
|
|
34
|
+ @Override
|
|
|
35
|
+ public void exportLedgerReport(Object params, Object dataList, File xlsxFile) {
|
|
|
36
|
+ List<LedgerReportExportModel> ledgerReportList = JsonUtil.parseList(JsonUtil.toJsonString(dataList), LedgerReportExportModel.class);
|
|
|
37
|
+ // 读取模板文件
|
|
|
38
|
+ ClassPathResource templateResource = new ClassPathResource("templates/ledgerReportExportTemplate.xlsx");
|
|
|
39
|
+ try (InputStream inputStream = templateResource.getStream();
|
|
|
40
|
+ Workbook workbook = new XSSFWorkbook(inputStream)) {
|
|
|
41
|
+ Sheet sheet = workbook.getSheetAt(0);
|
|
|
42
|
+ // 数据填充起始行
|
|
|
43
|
+ int rowIndex = 2;
|
|
|
44
|
+ for (int i = 0; i < ledgerReportList.size(); i++) {
|
|
|
45
|
+ LedgerReportExportModel model = ledgerReportList.get(i);
|
|
|
46
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 0, model.getQuota()
|
|
|
47
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
48
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 1, model.getSettleTerm()
|
|
|
49
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
50
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 2, model.getDeptName()
|
|
|
51
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
52
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 3, model.getRegionName()
|
|
|
53
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
54
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 4, model.getCustomerType()
|
|
|
55
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
56
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 5, model.getCustomerShortName()
|
|
|
57
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
58
|
+
|
|
|
59
|
+ List<ReceiptLedgerReportDetail> detailList = model.getDetailList();
|
|
|
60
|
+ if (CollectionUtils.isEmpty(detailList)) {
|
|
|
61
|
+ rowIndex++;
|
|
|
62
|
+ continue;
|
|
|
63
|
+ }
|
|
|
64
|
+ // 合并起始行
|
|
|
65
|
+ int startMergeIndex = rowIndex;
|
|
|
66
|
+ // 合并结束行
|
|
|
67
|
+ int endMergeIndex = rowIndex + detailList.size();
|
|
|
68
|
+ for (int j = 0; j < detailList.size(); j++) {
|
|
|
69
|
+ ReceiptLedgerReportDetail detail = detailList.get(j);
|
|
|
70
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 6, detail.getCustomerName()
|
|
|
71
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
72
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 7, detail.getDebtTotal()
|
|
|
73
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
74
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 8, detail.getAgreement()
|
|
|
75
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
76
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 9, detail.getFirstCoordinate()
|
|
|
77
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
78
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 10, detail.getSecondCoordinate()
|
|
|
79
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
80
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 11, detail.getClearDebt()
|
|
|
81
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
82
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 12, detail.getCreditStatus()
|
|
|
83
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
84
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 13, detail.getTimingStatus()
|
|
|
85
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
86
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 14, detail.getWaitDeliveredOrder()
|
|
|
87
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
88
|
+ rowIndex++;
|
|
|
89
|
+ }
|
|
|
90
|
+ if (detailList.size() > 1) {
|
|
|
91
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 6, "小计"
|
|
|
92
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
93
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 7, model.getDebtTotal()
|
|
|
94
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
95
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 8, model.getAgreementTotal()
|
|
|
96
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
97
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 9, model.getFirstCoordinateTotal()
|
|
|
98
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
99
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 10, model.getSecondCoordinateTotal()
|
|
|
100
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
101
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 11, model.getClearDebtTotal()
|
|
|
102
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
103
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 12, model.getCreditStatus()
|
|
|
104
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
105
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 13, model.getTimingStatus()
|
|
|
106
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
107
|
+ ExcelUtil.setCellValue(sheet, rowIndex, 14, model.getWaitDeliveredOrderTotal()
|
|
|
108
|
+ , setCellStyle(workbook, VerticalAlignment.CENTER, false));
|
|
|
109
|
+ // 合并单位格
|
|
|
110
|
+ CellRangeAddress cellRangeAddress1 = new CellRangeAddress(
|
|
|
111
|
+ startMergeIndex, endMergeIndex, 0, 0);
|
|
|
112
|
+ sheet.addMergedRegion(cellRangeAddress1);
|
|
|
113
|
+ CellRangeAddress cellRangeAddress2 = new CellRangeAddress(
|
|
|
114
|
+ startMergeIndex, endMergeIndex, 1, 1);
|
|
|
115
|
+ sheet.addMergedRegion(cellRangeAddress2);
|
|
|
116
|
+ CellRangeAddress cellRangeAddress3 = new CellRangeAddress(
|
|
|
117
|
+ startMergeIndex, endMergeIndex, 2, 2);
|
|
|
118
|
+ sheet.addMergedRegion(cellRangeAddress3);
|
|
|
119
|
+ CellRangeAddress cellRangeAddress4 = new CellRangeAddress(
|
|
|
120
|
+ startMergeIndex, endMergeIndex, 3, 3);
|
|
|
121
|
+ sheet.addMergedRegion(cellRangeAddress4);
|
|
|
122
|
+ CellRangeAddress cellRangeAddress5 = new CellRangeAddress(
|
|
|
123
|
+ startMergeIndex, endMergeIndex, 4, 4);
|
|
|
124
|
+ sheet.addMergedRegion(cellRangeAddress5);
|
|
|
125
|
+ CellRangeAddress cellRangeAddress6 = new CellRangeAddress(
|
|
|
126
|
+ startMergeIndex, endMergeIndex, 5, 5);
|
|
|
127
|
+ sheet.addMergedRegion(cellRangeAddress6);
|
|
|
128
|
+ rowIndex++;
|
|
|
129
|
+ }
|
|
|
130
|
+ }
|
|
|
131
|
+ // ========== 填充标题和表头 ==========
|
|
|
132
|
+ Map<String, Object> dataMap = new HashMap<>();
|
|
|
133
|
+ ReceiptLedgerReportVo reportVo = JsonUtil.parseObject(JsonUtil.toJsonString(params), ReceiptLedgerReportVo.class);
|
|
|
134
|
+ StringBuilder builder = new StringBuilder();
|
|
|
135
|
+ if (reportVo != null) {
|
|
|
136
|
+ SysDataDicItemService sysDataDicItemService = ApplicationUtil.getBean(SysDataDicItemService.class);
|
|
|
137
|
+ if (StringUtils.isNotBlank(reportVo.getDeptName())) {
|
|
|
138
|
+ builder.append(reportVo.getDeptName()).append(",");
|
|
|
139
|
+ }
|
|
|
140
|
+ if (StringUtils.isNotBlank(reportVo.getDebtAmountScope())) {
|
|
|
141
|
+ SysDataDicItem dicItem = sysDataDicItemService.findByCode("DEBT_AMOUNT_SCOPE", reportVo.getDebtAmountScope());
|
|
|
142
|
+ builder.append(dicItem.getName()).append(",");
|
|
|
143
|
+ }
|
|
|
144
|
+ if (StringUtils.isNotBlank(reportVo.getCustomerType())) {
|
|
|
145
|
+ SysDataDicItem dicItem = sysDataDicItemService.findByCode("ENTERPRISE_TYPE", reportVo.getCustomerType());
|
|
|
146
|
+ builder.append(dicItem.getName()).append(",");
|
|
|
147
|
+ }
|
|
|
148
|
+ if (StringUtils.isNotBlank(reportVo.getDebtStatus())) {
|
|
|
149
|
+ SysDataDicItem dicItem = sysDataDicItemService.findByCode("DEBT_STATUS", reportVo.getDebtStatus());
|
|
|
150
|
+ builder.append(dicItem.getName()).append(",");
|
|
|
151
|
+ }
|
|
|
152
|
+ if (builder.length() > 0) {
|
|
|
153
|
+ builder.deleteCharAt(builder.length() - 1);
|
|
|
154
|
+ }
|
|
|
155
|
+ }
|
|
|
156
|
+ dataMap.put("titleSuffix", "——" + builder.toString());
|
|
|
157
|
+ ExcelUtil.processTemplate(workbook, dataMap);
|
|
|
158
|
+ // 将workbook写入文件
|
|
|
159
|
+ try (FileOutputStream outputStream = new FileOutputStream(xlsxFile)) {
|
|
|
160
|
+ workbook.write(outputStream);
|
|
|
161
|
+ }
|
|
|
162
|
+ } catch (Exception e) {
|
|
|
163
|
+ log.error("台账报表导出失败:", e);
|
|
|
164
|
+ throw new DefaultClientException("台账报表导出失败:" + e.getMessage());
|
|
|
165
|
+ }
|
|
|
166
|
+ }
|
|
|
167
|
+
|
|
|
168
|
+ /**
|
|
|
169
|
+ * 设置单元格样式
|
|
|
170
|
+ *
|
|
|
171
|
+ * @param workbook 工作簿
|
|
|
172
|
+ * @param alignmentType 对齐方式
|
|
|
173
|
+ * @param setBorder 是否设置边框
|
|
|
174
|
+ * @return CellStyle
|
|
|
175
|
+ */
|
|
|
176
|
+ private CellStyle setCellStyle(Workbook workbook, VerticalAlignment alignmentType, boolean setBorder) {
|
|
|
177
|
+ // 创建字体
|
|
|
178
|
+ Font font = workbook.createFont();
|
|
|
179
|
+ font.setFontName("宋体");
|
|
|
180
|
+ font.setFontHeightInPoints((short) 11);
|
|
|
181
|
+ // 创建样式
|
|
|
182
|
+ CellStyle style = workbook.createCellStyle();
|
|
|
183
|
+ style.setFont(font);
|
|
|
184
|
+ // 对齐方式
|
|
|
185
|
+ style.setVerticalAlignment(alignmentType);
|
|
|
186
|
+ if (setBorder) {
|
|
|
187
|
+ // 设置边框
|
|
|
188
|
+ style.setBorderTop(BorderStyle.THIN);
|
|
|
189
|
+ style.setBorderRight(BorderStyle.THIN);
|
|
|
190
|
+ style.setBorderBottom(BorderStyle.THIN);
|
|
|
191
|
+ style.setBorderLeft(BorderStyle.THIN);
|
|
|
192
|
+ }
|
|
|
193
|
+ return style;
|
|
|
194
|
+ }
|
|
|
195
|
+} |
...
|
...
|
|