java poi 在excel中插入圖片

java web中導出excel數據是常見的功能,最近遇到一個需求是在excel中插入圖片。處理excel及其餘微軟辦公系列軟件經常使用的就是apache poi,它也是支持圖片插入的。插入圖片最主要的用到HSSFClientAnchor,文檔介紹以下:
public HSSFClientAnchor(int dx1,java

int dy1,
            int dx2,
            int dy2,
            short col1,
            int row1,
            short col2,
            int row2)

Creates a new client anchor and sets the top-left and bottom-right coordinates of the anchor. Note: Microsoft Excel seems to sometimes disallow higher y1 than y2 or higher x1 than x2, you might need to reverse them and draw shapes vertically or horizontally flipped!web

簡單的demo:apache

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;

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;

public class ExcelExport {

    public static void main(String[] args) {
         FileOutputStream fileOut = null;   
         BufferedImage bufferImg = null;   
        try {
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
          //加載圖片
            bufferImg = ImageIO.read(new File("e:/1.jpg"));   
            ImageIO.write(bufferImg, "jpg", byteArrayOut);
            HSSFWorkbook wb = new HSSFWorkbook();   
            HSSFSheet sheet1 = wb.createSheet("sheet1");  
            HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();   
            /**
                 dx1 - the x coordinate within the first cell.//定義了圖片在第一個cell內的偏移x座標,既左上角所在cell的偏移x座標,通常可設0
                dy1 - the y coordinate within the first cell.//定義了圖片在第一個cell的偏移y座標,既左上角所在cell的偏移y座標,通常可設0
                dx2 - the x coordinate within the second cell.//定義了圖片在第二個cell的偏移x座標,既右下角所在cell的偏移x座標,通常可設0
                dy2 - the y coordinate within the second cell.//定義了圖片在第二個cell的偏移y座標,既右下角所在cell的偏移y座標,通常可設0
                col1 - the column (0 based) of the first cell.//第一個cell所在列,既圖片左上角所在列
                row1 - the row (0 based) of the first cell.//圖片左上角所在行
                col2 - the column (0 based) of the second cell.//圖片右下角所在列
                row2 - the row (0 based) of the second cell.//圖片右下角所在行
             */
            HSSFClientAnchor anchor = new HSSFClientAnchor(-100, 0, 0, 0,(short) 2, 2, (short) 5, 8);   
            //插入圖片  
            patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); 
            fileOut = new FileOutputStream("e:/excel.xls");   
            // 輸出文件 
             wb.write(fileOut);   
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

關於dx1的設置的說明,dx2,dy1等都是相似的
圖片描述spa

關於一個excel設置多張圖片的demoexcel

package com.poi;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;

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;

public class ExcelExport {

    public static void main(String[] args) {
         FileOutputStream fileOut = null;   
         BufferedImage bufferImg = null;   
        try {
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
          //加載圖片
            bufferImg = ImageIO.read(new File("e:/1.jpg"));   
            ImageIO.write(bufferImg, "jpg", byteArrayOut);
            HSSFWorkbook wb = new HSSFWorkbook();   
            HSSFSheet sheet1 = wb.createSheet("sheet1");  
            HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();   
            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,(short) 2, 2, (short) 5, 8);
            //插入圖片 1 
            patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
            
            //圖片2
            anchor = new HSSFClientAnchor(200, 0, 0, 0,(short) 2, 9, (short) 5, 15);
            patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
            fileOut = new FileOutputStream("e:/excel.xls");   
            // 輸出文件 
             wb.write(fileOut);   
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

整體來講使用poi仍是很方便的。code

相關文章
相關標籤/搜索