|
@@ -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;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|