Browse Source

excel插入多个图片工具类

guoyong 4 years ago
parent
commit
8b33e4c573

+ 20 - 0
src/main/java/com/aoyang/tms/util/ExcelParam.java

@@ -0,0 +1,20 @@
+package com.aoyang.tms.util;
+
+import lombok.Data;
+
+/**
+ * @Description: 插入excel图片参数类
+ * @Author guoyong
+ * @Date 2022/4/26 15:54
+ * @Version 1.0
+ */
+@Data
+public class ExcelParam {
+
+    //签名图片地址
+    private String signUrl;
+    //插入excel的行位置
+    private int row;
+    //插入excel的列位置
+    private int cell;
+}

+ 81 - 0
src/main/java/com/aoyang/tms/util/ExcelUtils.java

@@ -0,0 +1,81 @@
+package com.aoyang.tms.util;
+
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.util.IOUtils;
+import org.apache.poi.xssf.usermodel.*;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @Description: excel工具类
+ * @Author guoyong
+ * @Date 2022/4/26 15:50
+ * @Version 1.0
+ */
+public class ExcelUtils {
+
+    public static void main(String[] args) {
+        String excelSrcPath = "d:\\1.xlsx";
+        String excelTargetPath = "d:\\2.xlsx";
+        List<ExcelParam> signs = new ArrayList<ExcelParam>();
+        ExcelParam excelParam1 = new ExcelParam();
+        excelParam1.setRow(44);
+        excelParam1.setCell(6);
+        excelParam1.setSignUrl("d:\\1.png");
+        signs.add(excelParam1);
+
+        ExcelParam excelParam2 = new ExcelParam();
+        excelParam2.setRow(44);
+        excelParam2.setCell(1);
+        excelParam2.setSignUrl("d:\\1.png");
+        signs.add(excelParam2);
+
+        int sheetIndex = 0;
+        try {
+            boolean flag = addImage2Excel(excelSrcPath, sheetIndex, signs, excelTargetPath);
+            System.out.println(flag);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * @return
+     * @Author guoyong
+     * @Description excel 插入多张图片
+     * @Date 2022/4/26
+     * @Param excelSrcPath 日志模板
+     * @Param sheetIndex sheet index
+     * @Param signs 多个图片url及插入地址
+     * @Param excelTargetPath 生成文件地址
+     **/
+    static boolean addImage2Excel(String excelSrcPath, int sheetIndex, List<ExcelParam> signs, String excelTargetPath) throws Exception {
+        boolean flag = false;
+        FileInputStream inputstream = new FileInputStream(new File(excelSrcPath));
+        XSSFWorkbook wb = new XSSFWorkbook(inputstream);
+        XSSFSheet sheet = wb.getSheetAt(sheetIndex);//获取第一个sheet
+        for (ExcelParam excelParam : signs) {
+            InputStream my_banner_image = new FileInputStream(excelParam.getSignUrl());
+            byte[] bytes = IOUtils.toByteArray(my_banner_image);
+            int my_picture_id = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
+            my_banner_image.close();
+            XSSFDrawing drawing = sheet.createDrawingPatriarch();
+            XSSFClientAnchor my_anchor = new XSSFClientAnchor();
+            my_anchor.setRow1(excelParam.getRow());
+            my_anchor.setCol1(excelParam.getCell());
+            XSSFPicture my_picture = drawing.createPicture(my_anchor, my_picture_id);
+            my_picture.resize();
+        }
+
+        /* Write changes to the workbook */
+        FileOutputStream out = new FileOutputStream(new File(excelTargetPath));
+        wb.write(out);
+        out.close();
+        return flag;
+    }
+}

+ 0 - 50
src/main/java/com/aoyang/tms/util/Picture.java

@@ -1,50 +0,0 @@
-package com.aoyang.tms.util;
-
-/**
- * @Description: ${DESCRIPTION}
- * @Author guoyong
- * @Date 2022/4/26 14:39
- * @Version 1.0
- */
-
-import java.io.*;
-
-import org.apache.poi.ss.usermodel.*;
-import org.apache.poi.xssf.usermodel.*;
-import org.apache.poi.ss.util.CellRangeAddress;
-import org.apache.poi.util.IOUtils;
-
-public class Picture {
-    public static void main(String[] args) throws Exception {
-        /* Create a Workbook and Worksheet */
-        FileInputStream inputstream = new FileInputStream(new File("D:\\2.xlsx"));
-        XSSFWorkbook wb = new XSSFWorkbook(inputstream);
-        XSSFSheet sheet = wb.getSheetAt(0);//获取第一个sheet
-        /* Read input PNG / JPG Image into FileInputStream Object*/
-        InputStream my_banner_image = new FileInputStream("d:\\1.png");
-        /* Convert picture to be added into a byte array */
-        byte[] bytes = IOUtils.toByteArray(my_banner_image);
-        /* Add Picture to Workbook, Specify picture type as PNG and Get an Index */
-        int my_picture_id = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
-        /* Close the InputStream. We are ready to attach the image to workbook now */
-        my_banner_image.close();
-        /* Create the drawing container */
-        XSSFDrawing drawing = sheet.createDrawingPatriarch();
-        /* Create an anchor point */
-        XSSFClientAnchor my_anchor = new XSSFClientAnchor();
-        /* Define top left corner, and we can resize picture suitable from there */
-        my_anchor.setCol1(6);
-        my_anchor.setRow1(43);
-        /* Invoke createPicture and pass the anchor point and ID */
-        XSSFPicture my_picture = drawing.createPicture(my_anchor, my_picture_id);
-        /* Call resize method, which resizes the image */
-        my_picture.resize();
-        /* Write changes to the workbook */
-        FileOutputStream out = new FileOutputStream(new File("D:\\3.xlsx"));
-        wb.write(out);
-        out.close();
-
-    }
-
-
-}