posts - 120,  comments - 19,  trackbacks - 0
          前面已經(jīng)講過利用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創(chuàng)建圖片,它接受二個參數(shù),第一個是HSSFClientAnchor,設(shè)定圖片的大小。

          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{
          ?????????? ??? ?
          ????????? //先把讀進(jìn)來的圖片放到一個ByteArrayOutputStream中,以便產(chǎn)生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);
          ???????? ?
          ??????? //創(chuàng)建一個工作薄
          ?? ??? ?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 阿成 閱讀(2995) 評論(4)  編輯  收藏 所屬分類: Open source
          主站蜘蛛池模板: 长武县| 罗平县| 海口市| 乌恰县| 达日县| 栖霞市| 泽普县| 合肥市| 普宁市| 城口县| 永胜县| 古丈县| 章丘市| 万州区| 资中县| 随州市| 葫芦岛市| 集安市| 甘泉县| 县级市| 获嘉县| 高碑店市| 绥江县| 政和县| 宜君县| 东辽县| 钦州市| 台东县| 常德市| 张掖市| 开化县| 雷州市| 沙湾县| 九台市| 津市市| 长沙县| 龙南县| 鄄城县| 信阳市| 来安县| 七台河市|