|
|
@@ -6,106 +6,44 @@ package com.aoyang.tms.util;
|
|
|
* @Date 2022/4/26 14:39
|
|
|
* @Version 1.0
|
|
|
*/
|
|
|
-import java.awt.image.BufferedImage;
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
|
|
|
-import javax.imageio.ImageIO;
|
|
|
+import java.io.*;
|
|
|
|
|
|
-import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
|
|
|
-import org.apache.poi.hssf.usermodel.HSSFPatriarch;
|
|
|
-import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
+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) {
|
|
|
- //String filepath = SpringConfigUtil.getValue("filepath");
|
|
|
- //String picturepath = SpringConfigUtil.getValue("picturepath");
|
|
|
- //int rowNum = Integer.parseInt(SpringConfigUtil.getValue("rowNum"));
|
|
|
- // int cellNum = Integer.parseInt(SpringConfigUtil.getValue("cellNum"));
|
|
|
- boolean result = insertPicture("D:\\日志.xlsx", "D:\\1.png", 3, 5);// 1代表插入的行数-1
|
|
|
- // ,2代表插入的列数-1
|
|
|
-
|
|
|
- System.out.println("图片插入结果为==" + result);
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ /* Create a Workbook and Worksheet */
|
|
|
+ XSSFWorkbook my_workbook = new XSSFWorkbook();
|
|
|
+ XSSFSheet my_sheet = my_workbook.createSheet("MyLogo");
|
|
|
+ /* 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 = my_workbook.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 = my_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(2);
|
|
|
+ my_anchor.setRow1(1);
|
|
|
+ /* 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:\\日志.xlsx"));
|
|
|
+ my_workbook.write(out);
|
|
|
+ out.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
- /* 插入图片地址 文件地址 插入图片位置的关键字 图片类型 */
|
|
|
-
|
|
|
- /**
|
|
|
- * @param filePath
|
|
|
- * @param picturePath
|
|
|
- * @param rowNum
|
|
|
- * @param cellNum
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static boolean insertPicture(String filePath, String picturePath, int rowNum, int cellNum) {
|
|
|
- // 初始化IO流内容
|
|
|
-
|
|
|
- FileOutputStream fileOut = null;
|
|
|
- BufferedImage bufferImg = null;
|
|
|
- int rowN = rowNum;// 图片插入行的初始化
|
|
|
- int cellN = cellNum;// 图片插入列的初始化
|
|
|
- try {
|
|
|
-
|
|
|
- // 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
|
|
|
- ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
|
|
|
-
|
|
|
- bufferImg = ImageIO.read(new File(picturePath));// 图片地址
|
|
|
-
|
|
|
- ImageIO.write(bufferImg, "png", byteArrayOut);
|
|
|
-
|
|
|
- // 创建一个工作薄
|
|
|
- HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filePath));
|
|
|
- HSSFSheet sheet1 = wb.getSheetAt(0);
|
|
|
-
|
|
|
- // 创建插入图片需要的容器
|
|
|
- HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
|
|
|
- /*
|
|
|
- * HSSFClientAnchor几个数字解释:3:是x轴的开始节点, 0:
|
|
|
- * 是y轴的开始节点,1023:是x轴的结束节点,255:是y轴的结束节点
|
|
|
- * ,1:是从Excel的2列开始插入图片,10:是从excel的第11行开始插入图片,
|
|
|
- * 11:图片占用11列的位置,25:图片结束在excel的26行
|
|
|
- */
|
|
|
- HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) ((short) cellN), (rowN),
|
|
|
- (short) ((short) cellN + 1), (rowN));
|
|
|
-
|
|
|
- anchor.setAnchorType(2);
|
|
|
-
|
|
|
- // 插入图片
|
|
|
-
|
|
|
- patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
|
|
|
|
|
|
- fileOut = new FileOutputStream(filePath);
|
|
|
- // 写入excel文件
|
|
|
- wb.write(fileOut);
|
|
|
- fileOut.close();
|
|
|
- return true;
|
|
|
- } catch (IOException io) {
|
|
|
-
|
|
|
- io.printStackTrace();
|
|
|
-
|
|
|
- System.out.println("io erorr : " + io.getMessage());
|
|
|
- return false;
|
|
|
-
|
|
|
- } finally {
|
|
|
-
|
|
|
- if (fileOut != null) {
|
|
|
-
|
|
|
- try {
|
|
|
-
|
|
|
- fileOut.close();
|
|
|
-
|
|
|
- } catch (IOException e) {
|
|
|
-
|
|
|
- e.printStackTrace();
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
}
|