POI里面处理图形HSSFWorkbook,ImageIO

POI里面处理图形或者图片的主要类是HSSFPatriarch,它负责管理一个表格里面所有的图片和图形,并且只能创建一个,如果你应用程序后来又创建了一个,那么将使以前创造的HSSFPatriarch所管理的图片和图形清除,所以一定要保留HSSFPatriarch的引用直到最后.

这些图片和单元格不同如果想取单元格上对应的图片咱么办,比如数据转移的时候遇到这样的问题
package com.org.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.net.URL;
import javax.imageio.ImageIO;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFSimpleShape;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

public class ExeclPicture{
    

    public ExeclPicture() {
     }
    public static void main(String[] args)throws Exception {
        //创建一个工作薄
         HSSFWorkbook wb=new HSSFWorkbook();
        //创建一个表格
         HSSFSheet sheet=wb.createSheet("sheet1");
        //创建一个列
         HSSFRow row=sheet.createRow(0);
        //创建一个样式
         HSSFCellStyle style=wb.createCellStyle();
        //设置这些样式
         style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
         style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
         style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
         style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
         style.setBorderRight(HSSFCellStyle.BORDER_THIN);
         style.setBorderTop(HSSFCellStyle.BORDER_THIN);
         style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //创建一个字体
         HSSFFont font=wb.createFont();
         font.setColor(HSSFColor.VIOLET.index);
         font.setFontHeightInPoints((short)16);
         font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //把字体应用到当前的样式
         style.setFont(font);
        //声明一个画图的顶级管理器
         HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
        //填充单元格
        for(int i=0;i<5;i++){
            //创建一个单元格
             HSSFCell cell=row.createCell(i);
            switch(i){
                case 0:
                    //设置普通文本
                     cell.setCellValue(new HSSFRichTextString("普通文本"));
                    break;
                case 1:
                    //设置为形状
                     HSSFClientAnchor a1 = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 1, 0, (short) 1, 0 );
                     HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
                    //这里可以设置形状的样式
                     shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
                    
                    break;
                case 2:
                    //设置为布尔量
                     cell.setCellValue(true);
                    break;
                case 3:
                    //设置为double值
                     cell.setCellValue(12.5);
                    break;
                case 4:
                    //设置为图片]
                     URL url=this.class.getResource("hello.jpg");
                     insertImage(wb,patriarch,getImageData(ImageIO.read(url)),0,4,1);
                    break;
                    
             }
            
            //设置单元格的样式
             cell.setCellStyle(style);
         }
         FileOutputStream fout=new FileOutputStream("我的第一个EXCEL.xls");
        //输出到文件
         wb.write(fout);
         fout.close();
     }
    //自定义的方法,插入某个图片到指定索引的位置
    private static void insertImage(HSSFWorkbook wb,HSSFPatriarch pa,byte[] data,int row,int column,int index){
        int x1=index*250;
        int y1=0;
        int x2=x1+255;
        int y2=255;
         HSSFClientAnchor anchor = new HSSFClientAnchor(x1,y1,x2,y2,(short)column,row,(short)column,row);
         anchor.setAnchorType(2);
         pa.createPicture(anchor , wb.addPicture(data,HSSFWorkbook.PICTURE_TYPE_JPEG));
     }
    //从图片里面得到字节数组
    private static  byte[] getImageData(BufferedImage bi){
        try{
             ByteArrayOutputStream bout=new ByteArrayOutputStream();
             ImageIO.write(bi,"PNG",bout);
            return bout.toByteArray();
         }catch(Exception exe){
             exe.printStackTrace();
            return null;
         }
     }
}

利用createPicture() 在patriarch上创建.支持 PNG ,JPG ,DIB 
一旦为某个wb的sheet创建了patriarch那么所有原有图片将取消,
通过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(); 
                     } 
               } 
            }
    }
}
 

你可能感兴趣的:(apache,工作,.net,Excel)