Commit 82b7b5ac91b8a89e079294687ac38c8b446af00c
1 parent
c94daedf
fix: 审批管理/待审批/已审批列表查询接口
Showing
6 changed files
with
122 additions
and
4 deletions
src/main/java/com/order/erp/common/excel4j/handler/SheetTemplateHandler.java
1 | 1 | package com.order.erp.common.excel4j.handler; |
2 | 2 | |
3 | 3 | import com.order.erp.common.excel4j.exceptions.Excel4JException; |
4 | +import org.apache.poi.hssf.usermodel.HSSFClientAnchor; | |
5 | +import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |
4 | 6 | import org.apache.poi.openxml4j.exceptions.InvalidFormatException; |
5 | 7 | import org.apache.poi.ss.usermodel.*; |
8 | +import org.apache.poi.xssf.streaming.SXSSFWorkbook; | |
9 | +import org.apache.poi.xssf.usermodel.XSSFClientAnchor; | |
10 | +import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |
6 | 11 | |
7 | 12 | import java.io.File; |
8 | 13 | import java.io.FileInputStream; |
... | ... | @@ -257,12 +262,57 @@ public class SheetTemplateHandler { |
257 | 262 | template.currentColumnIndex++; |
258 | 263 | return; |
259 | 264 | } |
265 | + | |
266 | + if (value instanceof byte[]) { | |
267 | + byte[] data = (byte[]) value; | |
268 | + // 5.1anchor主要用于设置图片的属性 | |
269 | + short x = (short) cell.getColumnIndex(); | |
270 | + int y = cell.getRowIndex(); | |
271 | + // 5.2插入图片 | |
272 | + addImage(cell.getSheet(), new Integer[]{y, y, (int) x, (int) x}, data); | |
273 | + cell.setCellValue(""); | |
274 | + template.currentColumnIndex++; | |
275 | + return; | |
276 | + } | |
277 | + | |
260 | 278 | // default value#toString |
261 | 279 | cell.setCellValue(value.toString()); |
262 | 280 | template.currentColumnIndex++; |
263 | 281 | } |
264 | 282 | |
265 | 283 | /** |
284 | + * 指定单元格,添加图片 | |
285 | + * | |
286 | + * @param position 下标位置, row start, row end, col start, col end | |
287 | + * @param images 图片数据 | |
288 | + */ | |
289 | + public static void addImage(Sheet sheet, Integer[] position, byte[] images) { | |
290 | + Drawing<?> drawingPatriarch = sheet.createDrawingPatriarch(); | |
291 | + Workbook workbook = sheet.getWorkbook(); | |
292 | + // 5.2插入图片 | |
293 | + ClientAnchor anchor; | |
294 | + int add1; | |
295 | + Integer startRow = position[0]; | |
296 | + Integer endRow = position[1] + 1; | |
297 | + Integer startCol = position[2]; | |
298 | + Integer endCol = position[3] + 1; | |
299 | + if (workbook instanceof XSSFWorkbook) { | |
300 | + anchor = new XSSFClientAnchor(0, 0, 0, 0, startCol, startRow, endCol, endRow); | |
301 | + anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE); | |
302 | + add1 = workbook.addPicture(images, XSSFWorkbook.PICTURE_TYPE_PNG); | |
303 | + } else if (workbook instanceof HSSFWorkbook) { | |
304 | + anchor = new HSSFClientAnchor(0, 0, 0, 0, startCol.shortValue(), startRow, endCol.shortValue(), endRow); | |
305 | + anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE); | |
306 | + add1 = workbook.addPicture(images, SXSSFWorkbook.PICTURE_TYPE_PNG); | |
307 | + } else { | |
308 | + anchor = new XSSFClientAnchor(0, 0, 0, 0, startCol, startRow, endCol, endRow); | |
309 | + anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE); | |
310 | + add1 = workbook.addPicture(images, XSSFWorkbook.PICTURE_TYPE_PNG); | |
311 | + } | |
312 | + drawingPatriarch.createPicture(anchor, add1); | |
313 | + } | |
314 | + | |
315 | + /** | |
266 | 316 | * 设置某个元素的样式 |
267 | 317 | * |
268 | 318 | * @param cell cell元素 | ... | ... |
src/main/java/com/order/erp/common/utils/FileUtil.java
... | ... | @@ -114,7 +114,7 @@ public class FileUtil extends cn.hutool.core.io.FileUtil { |
114 | 114 | /** |
115 | 115 | * inputStream 转 File |
116 | 116 | */ |
117 | - static File inputStreamToFile(InputStream ins, String name) throws Exception { | |
117 | + public static File inputStreamToFile(InputStream ins, String name) throws Exception { | |
118 | 118 | File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name); |
119 | 119 | if (file.exists()) { |
120 | 120 | return file; |
... | ... | @@ -132,6 +132,52 @@ public class FileUtil extends cn.hutool.core.io.FileUtil { |
132 | 132 | } |
133 | 133 | |
134 | 134 | /** |
135 | + * 将文件转换为byte数组,作为图片数据导入 | |
136 | + * | |
137 | + * @param file | |
138 | + * @return byte[] | |
139 | + */ | |
140 | + public static byte[] imageParseBytes(File file) { | |
141 | + FileInputStream fileInputStream = null; | |
142 | + try { | |
143 | + fileInputStream = new FileInputStream(file); | |
144 | + } catch (FileNotFoundException e) { | |
145 | + e.printStackTrace(); | |
146 | + } | |
147 | + return imageParseBytes(fileInputStream); | |
148 | + } | |
149 | + | |
150 | + /** | |
151 | + * 将流转换为byte数组,作为图片数据导入 | |
152 | + * | |
153 | + * @param fis | |
154 | + * @return byte[] | |
155 | + */ | |
156 | + public static byte[] imageParseBytes(InputStream fis) { | |
157 | + byte[] buffer = null; | |
158 | + ByteArrayOutputStream bos = null; | |
159 | + try { | |
160 | + bos = new ByteArrayOutputStream(1024); | |
161 | + byte[] b = new byte[1024]; | |
162 | + int n; | |
163 | + while ((n = fis.read(b)) != -1) { | |
164 | + bos.write(b, 0, n); | |
165 | + } | |
166 | + buffer = bos.toByteArray(); | |
167 | + } catch (Exception e) { | |
168 | + e.printStackTrace(); | |
169 | + } finally { | |
170 | + try { | |
171 | + fis.close(); | |
172 | + bos.close(); | |
173 | + } catch (IOException e) { | |
174 | + e.printStackTrace(); | |
175 | + } | |
176 | + } | |
177 | + return buffer; | |
178 | + } | |
179 | + | |
180 | + /** | |
135 | 181 | * 将文件名解析成文件的上传路径 |
136 | 182 | */ |
137 | 183 | public static File upload(MultipartFile file, String filePath) { | ... | ... |
src/main/java/com/order/erp/domain/dto/order/OrderFieldLockApplyDO.java
src/main/java/com/order/erp/domain/excel/OrderExcelVO.java
... | ... | @@ -68,9 +68,14 @@ public class OrderExcelVO implements Serializable { |
68 | 68 | /** |
69 | 69 | * pic图片地址 |
70 | 70 | */ |
71 | - @ExcelField(title = "pic图片地址", order = 11) | |
71 | +// @ExcelField(title = "pic图片地址", order = 11) | |
72 | 72 | private String picUrl; |
73 | 73 | /** |
74 | + * pic图片 | |
75 | + */ | |
76 | + @ExcelField(title = "pic图片", order = 11) | |
77 | + private byte[] picDate; | |
78 | + /** | |
74 | 79 | * 生产要求 |
75 | 80 | */ |
76 | 81 | @ExcelField(title = "生产要求", order = 12) | ... | ... |
src/main/java/com/order/erp/domain/vo/order/OrderApplyResultVO.java
src/main/java/com/order/erp/service/order/impl/OrderBaseInfoServiceImpl.java
... | ... | @@ -16,6 +16,7 @@ import com.order.erp.common.constant.ServerResultCode; |
16 | 16 | import com.order.erp.common.excel4j.ExcelUtils; |
17 | 17 | import com.order.erp.common.excel4j.exceptions.Excel4JException; |
18 | 18 | import com.order.erp.common.exception.BusinessException; |
19 | +import com.order.erp.common.utils.FileUtil; | |
19 | 20 | import com.order.erp.common.utils.OrderFieldUtils; |
20 | 21 | import com.order.erp.common.utils.ProfitUtils; |
21 | 22 | import com.order.erp.config.DataScope; |
... | ... | @@ -33,11 +34,13 @@ import com.order.erp.mapper.order.OrderBaseInfoMapper; |
33 | 34 | import com.order.erp.service.order.*; |
34 | 35 | import lombok.extern.slf4j.Slf4j; |
35 | 36 | import org.springframework.beans.BeanUtils; |
37 | +import org.springframework.beans.factory.annotation.Value; | |
36 | 38 | import org.springframework.stereotype.Service; |
37 | 39 | import org.springframework.transaction.annotation.Transactional; |
38 | 40 | |
39 | 41 | import javax.annotation.Resource; |
40 | 42 | import javax.servlet.http.HttpServletResponse; |
43 | +import java.io.File; | |
41 | 44 | import java.io.IOException; |
42 | 45 | import java.util.*; |
43 | 46 | import java.util.function.Function; |
... | ... | @@ -53,6 +56,11 @@ import java.util.stream.Collectors; |
53 | 56 | @Service |
54 | 57 | public class OrderBaseInfoServiceImpl extends ServiceImpl<OrderBaseInfoMapper, OrderBaseInfoDO> implements OrderBaseInfoService { |
55 | 58 | |
59 | + @Value("${file.path}") | |
60 | + private String path; | |
61 | + | |
62 | + @Value("${file.host}") | |
63 | + private String host; | |
56 | 64 | |
57 | 65 | @Resource |
58 | 66 | private OrderProfitAnalysisService profitAnalysisService; |
... | ... | @@ -184,6 +192,15 @@ public class OrderBaseInfoServiceImpl extends ServiceImpl<OrderBaseInfoMapper, O |
184 | 192 | List<OrderExcelVO> excelVOS = resultVOList.stream().map(x -> { |
185 | 193 | OrderExcelVO excelVO = new OrderExcelVO(); |
186 | 194 | BeanUtils.copyProperties(x, excelVO); |
195 | + if (StringUtils.isNotBlank(x.getPicUrl())) { | |
196 | + String filename = x.getPicUrl().replaceAll(host + "/images/", ""); | |
197 | + try { | |
198 | + excelVO.setPicDate(FileUtil.imageParseBytes(new File(path + File.separator + filename))); | |
199 | + } catch (Exception e) { | |
200 | + log.error("图片转换异常,订单:{},e:{}", x.getId(), e.getMessage()); | |
201 | + } | |
202 | + | |
203 | + } | |
187 | 204 | return excelVO; |
188 | 205 | }).collect(Collectors.toList()); |
189 | 206 | //response为HttpServletResponse对象 | ... | ... |