/*
* Test3.java
*
* Created on 2007年9月13日, 上午9:14
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package test1;
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;
/**
*
* @author hadeslee
*/
public class Test3 {
/** Creates a new instance of Test3 */
public Test3() {
}
public static void main(String[] args)throws Exception {
//聲明一個工作薄
HSSFWorkbook wb=new HSSFWorkbook();
//生成一個表格
HSSFSheet sheet=wb.createSheet("表格1");
//生成一個列
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(short 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=Test3.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;
}
}
}
* Test3.java
*
* Created on 2007年9月13日, 上午9:14
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package test1;
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;
/**
*
* @author hadeslee
*/
public class Test3 {
/** Creates a new instance of Test3 */
public Test3() {
}
public static void main(String[] args)throws Exception {
//聲明一個工作薄
HSSFWorkbook wb=new HSSFWorkbook();
//生成一個表格
HSSFSheet sheet=wb.createSheet("表格1");
//生成一個列
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(short 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=Test3.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;
}
}
}
POI里面處理圖形或者圖片的主要類是HSSFPatriarch,它負責管理一個表格里面所有的圖片和圖形,并且只能創建一個,如果你應用程序后來又創建了一個,那么將使以前創造的HSSFPatriarch所管理的圖片和圖形清除,所以一定要保留HSSFPatriarch的引用直到最后.
盡管千里冰封
依然擁有晴空
你我共同品味JAVA的濃香.