浏览代码

产品库导出Excel

yzc 1 年之前
父节点
当前提交
de95275566

+ 2 - 3
hx-item/src/main/java/com/fjhx/item/entity/product/bo/ProductExcelExportBo.java

@@ -3,18 +3,17 @@ package com.fjhx.item.entity.product.bo;
 import com.alibaba.excel.annotation.ExcelProperty;
 import com.alibaba.excel.annotation.write.style.ColumnWidth;
 import com.alibaba.excel.annotation.write.style.ContentRowHeight;
+import com.alibaba.excel.metadata.data.WriteCellData;
 import lombok.Getter;
 import lombok.Setter;
 
-import java.net.URL;
-
 @Getter
 @Setter
 @ContentRowHeight(50)
 public class ProductExcelExportBo {
     @ColumnWidth(10)
     @ExcelProperty("产品图片")
-    private URL productImg;
+    private WriteCellData<Void> productImg;
     @ColumnWidth(15)
     @ExcelProperty("产品编号")
     private String productCode;

+ 23 - 9
hx-item/src/main/java/com/fjhx/item/service/product/impl/ProductInfoServiceImpl.java

@@ -3,6 +3,7 @@ package com.fjhx.item.service.product.impl;
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.excel.metadata.data.WriteCellData;
 import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.dynamic.datasource.annotation.DSTransactional;
 import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
@@ -33,6 +34,7 @@ import com.fjhx.item.service.product.ProductClassifyService;
 import com.fjhx.item.service.product.ProductInfoService;
 import com.fjhx.item.service.product.ProductStockInfoService;
 import com.fjhx.item.util.CodeEnum;
+import com.fjhx.item.util.excel.ExcelImgUtils;
 import com.fjhx.item.util.excel.util.ExcelUtil;
 import com.fjhx.tenant.entity.dict.dto.DictTenantDataSelectDto;
 import com.fjhx.tenant.entity.dict.vo.DictTenantDataVo;
@@ -51,10 +53,13 @@ import org.apache.commons.collections4.CollectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import javax.imageio.ImageIO;
 import javax.servlet.http.HttpServletResponse;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.IOException;
 import java.math.BigDecimal;
-import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.*;
 import java.util.function.Function;
@@ -792,19 +797,28 @@ public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, Produ
             width = ObjectUtil.isEmpty(width) ? BigDecimal.ZERO : width;
             height = ObjectUtil.isEmpty(height) ? BigDecimal.ZERO : height;
 
+            ProductExcelExportBo bo = new ProductExcelExportBo();
             //获取产品图片信息
-            String fileUrl = "";
             List<FileInfoVo> fileInfoVoList = fileMap.get(productInfo.getId());
             if (ObjectUtil.isNotEmpty(fileInfoVoList)) {
-                fileUrl = fileInfoVoList.get(0).getFileUrl();
+                try {
+                    String fileUrl = fileInfoVoList.get(0).getFileUrl();
+                    BufferedImage image = ImageIO.read(new URL(fileUrl));
+                    if (ObjectUtil.isNotEmpty(image)) {
+                        //获取图片宽高,用以计算比例
+                        Double imgWidth = Double.valueOf(image.getWidth());
+                        Double imgHeight = Double.valueOf(image.getHeight());
+                        //------------------
+                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                        ImageIO.write(image, "png", baos);
+                        WriteCellData<Void> voidWriteCellData = ExcelImgUtils.imageCells(baos.toByteArray(), imgWidth, imgHeight, 10.0 * 5.8, 50.0);
+                        bo.setProductImg(voidWriteCellData);
+                    }
+                } catch (IOException e) {
+                    throw new ServiceException("导出失败" + e.getMessage());
+                }
             }
 
-            ProductExcelExportBo bo = new ProductExcelExportBo();
-            try {
-                bo.setProductImg(new URL(fileUrl));
-            } catch (MalformedURLException e) {
-                throw new RuntimeException(e);
-            }
             bo.setProductCode(productInfo.getCustomCode());
             bo.setProductName(productInfo.getName());
             bo.setProductSize(String.format("%s * %s * %s", length, width, height));

+ 60 - 0
hx-item/src/main/java/com/fjhx/item/util/excel/ExcelImgUtils.java

@@ -0,0 +1,60 @@
+package com.fjhx.item.util.excel;
+
+import com.alibaba.excel.metadata.data.ImageData;
+import com.alibaba.excel.metadata.data.WriteCellData;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ExcelImgUtils {
+    /**
+     * Excel图片等比例缩放
+     */
+    public static WriteCellData<Void> imageCells(byte[] bytes, Double imageWidth, Double imageHight, Double cellWidth, Double cellHeight) {
+        //获取屏幕最短边
+        double cellShort = cellWidth > cellHeight ? cellHeight : cellWidth;
+        //获取图片最长边
+        double imgLength = imageWidth > imageHight ? imageWidth : imageHight;
+        //图片最长边/屏幕最短边 计算比例
+        double aa = imgLength / cellShort;
+        //计算最终大小
+        double outImgWidth = imageWidth / aa;
+        double outImgHeight = imageHight / aa;
+        //计算边界距离
+        double top1 = (cellHeight - outImgHeight) / 2;
+        double left1 = (cellWidth - outImgWidth) / 2;
+
+        Integer top = Math.toIntExact(Math.round(top1));
+        Integer left = Math.toIntExact(Math.round(left1));
+
+        WriteCellData<Void> writeCellData = new WriteCellData<>();
+        // 这里可以设置为 EMPTY 则代表不需要其他数据了
+        //writeCellData.setType(CellDataTypeEnum.EMPTY);
+
+        // 可以放入多个图片
+        List<ImageData> imageDataList = new ArrayList<>();
+        writeCellData.setImageDataList(imageDataList);
+
+        ImageData imageData = new ImageData();
+        imageDataList.add(imageData);
+        // 设置图片
+        imageData.setImage(bytes);
+        // 图片类型
+        //imageData.setImageType(ImageData.ImageType.PICTURE_TYPE_PNG);
+        // 上 右 下 左 需要留空,这个类似于 css 的 margin;这里实测 不能设置太大 超过单元格原始大小后 打开会提示修复。暂时未找到很好的解法。
+        imageData.setTop(top);
+        imageData.setRight(left);
+        imageData.setBottom(top);
+        imageData.setLeft(left);
+
+        // * 设置图片的位置。Relative表示相对于当前的单元格index。first是左上点,last是对角线的右下点,这样确定一个图片的位置和大小。
+        // 目前填充模板的图片变量是images,index:row=7,column=0。所有图片都基于此位置来设置相对位置
+        // 第1张图片相对位置
+        imageData.setRelativeFirstRowIndex(0);
+        imageData.setRelativeFirstColumnIndex(0);
+        imageData.setRelativeLastRowIndex(0);
+        imageData.setRelativeLastColumnIndex(0);
+
+        return writeCellData;
+    }
+}