POI里面处理图形或者图片的主要类是HSSFPatriarch,它负责管理一个表格里面所有的图片和图形,并且只能创建一个,如果你应用程序后来又创建了一个,那么将使以前创造的HSSFPatriarch所管理的图片和图形清除,所以一定要保留HSSFPatriarch的引用直到最后.
这些图片和单元格不同如果想取单元格上对应的图片咱么办,比如数据转移的时候遇到这样的问题
1package com.org.util;
2
3import java.awt.image.BufferedImage;
4import java.io.ByteArrayOutputStream;
5import java.io.FileOutputStream;
6import java.net.URL;
7import javax.imageio.ImageIO;
8import org.apache.poi.hssf.usermodel.HSSFCell;
9import org.apache.poi.hssf.usermodel.HSSFCellStyle;
10import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
11import org.apache.poi.hssf.usermodel.HSSFFont;
12import org.apache.poi.hssf.usermodel.HSSFPatriarch;
13import org.apache.poi.hssf.usermodel.HSSFRichTextString;
14import org.apache.poi.hssf.usermodel.HSSFRow;
15import org.apache.poi.hssf.usermodel.HSSFSheet;
16import org.apache.poi.hssf.usermodel.HSSFSimpleShape;
17import org.apache.poi.hssf.usermodel.HSSFWorkbook;
18import org.apache.poi.hssf.util.HSSFColor;
19
20public class ExeclPicture{
21
22
23 public ExeclPicture() {
24 }
25 public static void main(String[] args)throws Exception {
26 //创建一个工作薄
27 HSSFWorkbook wb=new HSSFWorkbook();
28 //创建一个表格
29 HSSFSheet sheet=wb.createSheet("sheet1");
30 //创建一个列
31 HSSFRow row=sheet.createRow(0);
32 //创建一个样式
33 HSSFCellStyle style=wb.createCellStyle();
34 //设置这些样式
35 style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
36 style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
37 style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
38 style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
39 style.setBorderRight(HSSFCellStyle.BORDER_THIN);
40 style.setBorderTop(HSSFCellStyle.BORDER_THIN);
41 style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
42 //创建一个字体
43 HSSFFont font=wb.createFont();
44 font.setColor(HSSFColor.VIOLET.index);
45 font.setFontHeightInPoints((short)16);
46 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
47 //把字体应用到当前的样式
48 style.setFont(font);
49 //声明一个画图的顶级管理器
50 HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
51 //填充单元格
52 for(int i=0;i<5;i++){
53 //创建一个单元格
54 HSSFCell cell=row.createCell(i);
55 switch(i){
56 case 0:
57 //设置普通文本
58 cell.setCellValue(new HSSFRichTextString("普通文本"));
59 break;
60 case 1:
61 //设置为形状
62 HSSFClientAnchor a1 = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 1, 0, (short) 1, 0 );
63 HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
64 //这里可以设置形状的样式
65 shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
66
67 break;
68 case 2:
69 //设置为布尔量
70 cell.setCellValue(true);
71 break;
72 case 3:
73 //设置为double值
74 cell.setCellValue(12.5);
75 break;
76 case 4:
77 //设置为图片]
78 URL url=this.class.getResource("hello.jpg");
79 insertImage(wb,patriarch,getImageData(ImageIO.read(url)),0,4,1);
80 break;
81
82 }
83
84 //设置单元格的样式
85 cell.setCellStyle(style);
86 }
87 FileOutputStream fout=new FileOutputStream("我的第一个EXCEL.xls");
88 //输出到文件
89 wb.write(fout);
90 fout.close();
91 }
92 //自定义的方法,插入某个图片到指定索引的位置
93 private static void insertImage(HSSFWorkbook wb,HSSFPatriarch pa,byte[] data,int row,int column,int index){
94 int x1=index*250;
95 int y1=0;
96 int x2=x1+255;
97 int y2=255;
98 HSSFClientAnchor anchor = new HSSFClientAnchor(x1,y1,x2,y2,(short)column,row,(short)column,row);
99 anchor.setAnchorType(2);
100 pa.createPicture(anchor , wb.addPicture(data,HSSFWorkbook.PICTURE_TYPE_JPEG));
101 }
102 //从图片里面得到字节数组
103 private static byte[] getImageData(BufferedImage bi){
104 try{
105 ByteArrayOutputStream bout=new ByteArrayOutputStream();
106 ImageIO.write(bi,"PNG",bout);
107 return bout.toByteArray();
108 }catch(Exception exe){
109 exe.printStackTrace();
110 return null;
111 }
112 }
113}
114
利用createPicture() 在patriarch上创建.支持 PNG ,JPG ,DIB
一旦为某个wb的sheet创建了patriarch那么所有原有图片将取消,
通过HSSFPatriarch类createPicture方法的在指定的wb中的sheet创建图片,它接受二个参数,第一个是HSSFClientAnchor,设定图片的大小。
1package com.poi.hssf.test;
2
3import java.io.FileOutputStream;
4import java.io.File;
5import java.io.ByteArrayOutputStream;
6import java.io.IOException;
7
8import java.awt.image.BufferedImage;
9import javax.imageio.*;
10
11import org.apache.poi.hssf.usermodel.HSSFWorkbook;
12import org.apache.poi.hssf.usermodel.HSSFSheet;
13import org.apache.poi.hssf.usermodel.HSSFPatriarch;
14import org.apache.poi.hssf.usermodel.HSSFClientAnchor;;
15
16public class TestPOI {
17
18 public static void main(String[] args) {
19 FileOutputStream fileOut = null;
20 BufferedImage bufferImg =null;
21 BufferedImage bufferImg1 = null;
22 try{
23
24 //先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
25 ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
26 ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
27 bufferImg = ImageIO.read(new File("d:/PieChart.jpg"));
28 bufferImg1 = ImageIO.read(new File("d:/fruitBarChart.jpg"));
29 ImageIO.write(bufferImg,"jpg",byteArrayOut);
30 ImageIO.write(bufferImg1,"jpg",byteArrayOut1);
31
32 //创建一个工作薄
33 HSSFWorkbook wb = new HSSFWorkbook();
34 HSSFSheet sheet1 = wb.createSheet("new sheet");
35 //HSSFRow row = sheet1.createRow(2);
36 HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
37 HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) 1,1,(short)10,20);
38 HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,512,255,(short) 2,30,(short)10,60);
39 anchor1.setAnchorType(2);
40 //插入图片
41 patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
42 patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
43
44 fileOut = new FileOutputStream("d:/workbook.xls");
45 //写入excel文件
46 wb.write(fileOut);
47 fileOut.close();
48
49 }catch(IOException io){
50 io.printStackTrace();
51 System.out.println("io erorr : "+ io.getMessage());
52 } finally
53 {
54 if (fileOut != null)
55 {
56
57 try {
58 fileOut.close();
59 }
60 catch (IOException e)
61 {
62 // TODO Auto-generated catch block
63 e.printStackTrace();
64 }
65 }
66 }
67 }
68}
69
posted on 2008-11-25 10:52
Vincent-chen 阅读(8579)
评论(2) 编辑 收藏 所属分类:
POI 、
Print