1. 程式人生 > 實用技巧 >JAVA將圖片寫入到Excel檔案中工具類

JAVA將圖片寫入到Excel檔案中工具類

前言  

  此方法是利用了poi讀取excel然後將其寫入到excel指定單元格位置處,並且圖片在excle中是處於懸浮狀態的,圖片最終展示的大小為指定單元格的大小

匯入poi相關jar包:

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </
dependency>

工具類程式碼:

package test;

import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*; import java.util.HashMap; import java.util.Map; public class WriteImgUtil { /** * 寫入圖片到Excel指定的位置 * * @param patriarch 畫圖的頂級管理器,一個sheet只能獲取一次,多次插入圖片請使用同一個patriarch物件 * @param wb HSSFWorkbook物件 * @param filepath 圖片檔案路徑 * @param cellPoint 自定義的物件,指定要插入圖片的座標(x, y) *
@return cellPoint 自定義的物件,返回下一個要插入圖片的座標(x, y) * @throws IOException */ public static void whiteImg(XSSFDrawing patriarch, XSSFWorkbook wb, String filepath, CellPoint cellPoint) throws IOException { ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); BufferedImage bufferImg = ImageIO.read(new File(filepath)); ImageIO.write(bufferImg, filepath.substring(filepath.lastIndexOf(".") + 1), byteArrayOut); // 起點座標 int x1 = cellPoint.getRow1(); int y1 = cellPoint.getCol1(); // 終點座標 int x2 = cellPoint.getRow2(); int y2 = cellPoint.getCol2(); // anchor主要用於設定圖片的屬性 XSSFClientAnchor anchor1 = new XSSFClientAnchor(0, 0, 0, 0, (short) x1, y1, (short) x2, y2); // 插入圖片 int index = wb.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PICT); patriarch.createPicture(anchor1, index); } public static class CellPoint { // 起點橫座標 private int row1; // 起點縱座標 private int col1; // 終點橫座標 private int row2; // 終點縱座標 private int col2; public CellPoint(int row1, int col1, int row2, int col2) { this.row1 = row1; this.col1 = col1; this.row2 = row2; this.col2 = col2; } public int getRow1() { return row1; } public void setRow1(int row1) { this.row1 = row1; } public int getCol1() { return col1; } public void setCol1(int col1) { this.col1 = col1; } public int getRow2() { return row2; } public void setRow2(int row2) { this.row2 = row2; } public int getCol2() { return col2; } public void setCol2(int col2) { this.col2 = col2; } } /** * * 插入圖片到指定excel * * @param file 待插入excel檔案 * @param sheetIndex 待插入excel的Sheet序號(從0開始) * @param map key: 待插入圖片路徑 value: 圖片在excel中的座標 * @return * @Date: 2020/11/28 10:09 */ public static void addImageToExcel(File file, Integer sheetIndex, Map<String, CellPoint> map) { FileOutputStream fileOut = null; try { FileInputStream inputstream = new FileInputStream(file); XSSFWorkbook wb = new XSSFWorkbook(inputstream); XSSFSheet sheet1 = wb.getSheetAt(sheetIndex); // 畫圖的頂級管理器,一個sheet只能獲取一個(一定要注意這點) XSSFDrawing patriarch = sheet1.createDrawingPatriarch(); for (Map.Entry<String, CellPoint> entry : map.entrySet()) { String key = entry.getKey(); // 圖片路徑 CellPoint point = entry.getValue(); // 圖片插入座標 whiteImg(patriarch, wb, key, point); } fileOut = new FileOutputStream(file); // 寫入excel檔案 wb.write(fileOut); } catch (Exception e) { e.printStackTrace(); } finally { if (fileOut != null) { try { fileOut.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) throws IOException { Map<String, CellPoint> map = new HashMap<>(); map.put("D:\\1.png",new CellPoint(5,13,7,14)); addImageToExcel(new File("D:\\2.xlsx"),0,map); System.out.println("ok"); } }