POI导出图片(HSSFWorkbook和SXSSFWorkbook两种导出方式)

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

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;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class test {
    public static void main(String[] args) {
        test1();
    }

    // HSSFWorkbook 导出图片
    public static void test1() {
        FileOutputStream fileOut = null;
        BufferedImage bufferImg = null;
        // 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
        try {
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
            bufferImg = ImageIO.read(new File("E:/1/2.png"));
            ImageIO.write(bufferImg, "png", byteArrayOut);

            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet1 = wb.createSheet("test picture");
            // 画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
            HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
            // anchor主要用于设置图片的属性
            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255, (short) 1, 1, (short) 5, 8);
            anchor.setAnchorType(3);
            // 插入图片
            patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
            // 写入excel文件
            fileOut = new FileOutputStream("E:/测试Excel.xls");
            wb.write(fileOut);
            System.out.println("----Excle文件已生成------");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOut != null) {
                try {
                    fileOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void test2() {

        FileOutputStream fileOut = null;
        BufferedImage bufferImg = null;
        // 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
        try {
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
            bufferImg = ImageIO.read(new File("E:/1/2.png"));
            ImageIO.write(bufferImg, "png", byteArrayOut);

            SXSSFWorkbook wb = new SXSSFWorkbook();
            SXSSFSheet sheet1 = wb.createSheet("test picture");
            
            // 图片绝对路径
            BufferedImage user_headImg = ImageIO
                    .read(new File("E:/1/2.png"));
            ImageIO.write(user_headImg, "png", byteArrayOut);
            // sheet只能获取一个
            Drawing patriarch = sheet1.createDrawingPatriarch();
            // 设置图片的属性
            XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 1023, 255, (short) 6, 3, (short) 8, 7);
            // 插入图片
            patriarch.createPicture(anchor,
                    wb.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG));
            // 写入excel文件
            fileOut = new FileOutputStream("E:/测试Excel.xlsx");
            wb.write(fileOut);
            System.out.println("----Excle文件已生成------");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOut != null) {
                try {
                    fileOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

你可能感兴趣的:(POI,poi,excel)