posts - 120,  comments - 19,  trackbacks - 0
前面已经讲过利用POI读写Excel,下面是一个用POI向Excel中插入图片的例子。

官方文档:
Images are part of the drawing support. To add an image just call createPicture() on the drawing patriarch. At the time of writing the following types are supported:
PNG
JPG
DIB
It is not currently possible to read existing images and it should be noted that any existing drawings may be erased once you add a image to a sheet.

// Create the drawing patriarch. This is the top level container for
// all shapes. This will clear out any existing shapes for that sheet.


通过HSSFPatriarch类createPicture方法的在指定的wb中的sheet创建图片,它接受二个参数,第一个是HSSFClientAnchor,设定图片的大小。

package com.poi.hssf.test;

import java.io.FileOutputStream;
import java.io.File;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.awt.image.BufferedImage;
import javax.imageio.*;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;;

public class TestPOI {

    public static void main(String[] args) {
            FileOutputStream fileOut = null;
            BufferedImage bufferImg =null;
            BufferedImage bufferImg1 = null;
            try{
                
          //先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
          ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
          ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
          bufferImg = ImageIO.read(new File("d:/PieChart.jpg"));
          bufferImg1 = ImageIO.read(new File("d:/fruitBarChart.jpg"));
          ImageIO.write(bufferImg,"jpg",byteArrayOut);
          ImageIO.write(bufferImg1,"jpg",byteArrayOut1);
          
        //创建一个工作薄
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet1 = wb.createSheet("new sheet");
        //HSSFRow row = sheet1.createRow(2);
        HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
        HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) 1,1,(short)10,20);
        HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,512,255,(short) 2,30,(short)10,60);
        anchor1.setAnchorType(2);
        //插入图片
        patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
        patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
        
            fileOut = new FileOutputStream("d:/workbook.xls");
            //写入excel文件
            wb.write(fileOut);
            fileOut.close();
        
            }catch(IOException io){
                    io.printStackTrace();
                    System.out.println("io erorr :  "+ io.getMessage());
            } finally
            {
                if (fileOut != null)
                {
                           
                    try {
                              fileOut.close();
                         }
                    catch (IOException e)
                    {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                     }
                }
            }
    }
}



posted on 2006-08-16 09:09 阿成 阅读(2969) 评论(4)  编辑  收藏 所属分类: Open source

只有注册用户登录后才能发表评论。


网站导航: