buhuazhen
2026-05-21 06df28e9434e6b6287aa7aacf42a8a2e00f31454
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
package com.ruoyi.basic.excel;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.production.dto.ExportProductionPrintOrderDto;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Component;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
 
/**
 * @author buhuazhen
 * @date 2026/4/25
 * @email 3038525872@qq.com
 */
@Component
public class ProductionPrintOrderExcel {
 
 
    /**
     * 合并单元格并为合并区域添加左右边框
     *
     * @param sheet    Sheet
     * @param workbook Workbook
     * @param range    要合并的区域
     */
    public static void mergeWithBorder(Sheet sheet, Workbook workbook, CellRangeAddress range, CellStyle s) {
 
        // 1. 添加合并区域(检测是否重叠)
        for (int i = sheet.getNumMergedRegions() - 1; i >= 0; i--) {
            CellRangeAddress old = sheet.getMergedRegion(i);
            // 判断是否重叠(简单矩形重叠逻辑)
            if (!(range.getLastRow() < old.getFirstRow()
                    || range.getFirstRow() > old.getLastRow()
                    || range.getLastColumn() < old.getFirstColumn()
                    || range.getFirstColumn() > old.getLastColumn())) {
 
                // 存在重叠 → 删除旧的
                sheet.removeMergedRegion(i);
            }
        }
 
        sheet.addMergedRegion(range);
 
        // 2. 为合并区域内的所有单元格添加左右边框
        for (int rowIdx = range.getFirstRow(); rowIdx <= range.getLastRow(); rowIdx++) {
            Row row = sheet.getRow(rowIdx);
            if (row == null) {
                row = sheet.createRow(rowIdx);
            }
 
            for (int colIdx = range.getFirstColumn(); colIdx <= range.getLastColumn(); colIdx++) {
 
                Cell cell = row.getCell(colIdx);
                if (cell == null) {
                    cell = row.createCell(colIdx);
                }
 
                CellStyle style = workbook.createCellStyle();
 
                // 若该单元格已有样式,复制
                if (cell.getCellStyle() != null) {
                    style.cloneStyleFrom(cell.getCellStyle());
                }
 
                // 设置左右边框
                style.setBorderLeft(s.getBorderLeft());
                style.setBorderRight(s.getBorderRight());
                style.setBorderTop(s.getBorderTop());
                style.setBorderBottom(s.getBorderBottom());
                style.setAlignment(s.getAlignment());
                style.setVerticalAlignment(VerticalAlignment.CENTER);
                cell.setCellStyle(style);
            }
        }
    }
    @SneakyThrows
    public byte[] createPrintOrderTemplate(ExportProductionPrintOrderDto printOrderDto, InputStream templateInputStream) {
 
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 
        try (ExcelWriter excelWriter = EasyExcel.write(byteArrayOutputStream)
                .withTemplate(templateInputStream)
                .build()) {
 
            FillConfig fillConfig = FillConfig.builder()
                    .forceNewRow(true)
                    .autoStyle(true)
                    .direction(WriteDirectionEnum.VERTICAL)
                    .build();
 
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
 
            excelWriter.fill(printOrderDto, writeSheet);
 
            excelWriter.fill(new FillWrapper("materialInfo", printOrderDto.getMaterialInfo()), fillConfig, writeSheet);
            excelWriter.fill(new FillWrapper("processContent", printOrderDto.getProcessContent()), fillConfig, writeSheet);
            excelWriter.fill(new FillWrapper("plateMaking", printOrderDto.getPlateMaking()), fillConfig, writeSheet);
 
            excelWriter.finish();
 
        } catch (Exception e) {
            throw new ServiceException("excel 生成失败! " + e.getMessage());
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            Workbook workbook = WorkbookFactory.create(
                    new ByteArrayInputStream(byteArrayOutputStream.toByteArray())
            );
 
            CellStyle cellStyleTblr = workbook.createCellStyle();
            cellStyleTblr.setBorderBottom(BorderStyle.THIN);
            cellStyleTblr.setBorderTop(BorderStyle.THIN);
            cellStyleTblr.setBorderLeft(BorderStyle.THIN);
            cellStyleTblr.setBorderRight(BorderStyle.THIN);
            cellStyleTblr.setWrapText(true);
            cellStyleTblr.setAlignment(HorizontalAlignment.CENTER);
            cellStyleTblr.setVerticalAlignment(VerticalAlignment.CENTER);
 
            Sheet sheet = workbook.getSheetAt(0);
 
            // ===== 插入图片 =====
            byte[] imageBytes = printOrderDto.getCuttingImage();
 
            int pictureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_JPEG);
            CreationHelper helper = workbook.getCreationHelper();
            Drawing<?> drawing = sheet.createDrawingPatriarch();
 
            ClientAnchor anchor = helper.createClientAnchor();
            anchor.setCol1(6);  // 第7列(G列)
            anchor.setRow1(7);
            anchor.setDx1(142700); // 向右移动(约半格)
            anchor.setDy1(222700);
 
            anchor.setCol2(8);  // 宽度终点
            anchor.setRow2(9);  // 高度终点
 
            Picture picture = drawing.createPicture(anchor, pictureIdx);
//            picture.resize(0.5);
 
 
 
            // 进行合并操作 切料示意图部分
            int startRow = 7;
            int size = printOrderDto.getMaterialInfo().size();
            int qlEndRow = startRow + size - 1 + 4;
            mergeWithBorder(sheet, workbook, new CellRangeAddress(
                    startRow,
                    qlEndRow,
                    6,
                    7
            ), cellStyleTblr);
 
            // 产品备注
            mergeWithBorder(sheet, workbook, new CellRangeAddress(
                    6+1+printOrderDto.getMaterialInfo().size() +0,
                    6+1+printOrderDto.getMaterialInfo().size() +1,
                    0,
                    5
            ), cellStyleTblr);
 
            int zbRowStart = qlEndRow + 2 + CollUtil.size(printOrderDto.getPlateMaking()) +1;
            int zbRowEnd = zbRowStart + CollUtil.size(printOrderDto.getProcessContent());
            mergeWithBorder(sheet, workbook, new CellRangeAddress(
                    zbRowStart,
                    zbRowEnd,
                    4,
                    7
            ), cellStyleTblr);
 
 
            workbook.write(out);
            workbook.close();
        } catch (Exception e) {
            throw new ServiceException("图片处理失败! " + e.getMessage());
        }
 
        return out.toByteArray();
    }
 
}