码迷,mamicode.com
首页 > 编程语言 > 详细

Java向Excel中插入图片

时间:2015-10-12 18:41:38      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

Java向Excel中插入图片

  1. import java.io.FileOutputStream;   
  2. import java.io.File;   
  3. import java.io.ByteArrayOutputStream;   
  4. import java.io.IOException;   
  5.   
  6. import java.awt.image.BufferedImage;   
  7. import javax.imageio.*;   
  8.   
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  10. import org.apache.poi.hssf.usermodel.HSSFSheet;   
  11. import org.apache.poi.hssf.usermodel.HSSFPatriarch;   
  12. import org.apache.poi.hssf.usermodel.HSSFClientAnchor;;   
  13.   
  14. public class TestPOI {   
  15.   
  16.     public static void main(String[] args) {   
  17.             FileOutputStream fileOut = null;   
  18.             BufferedImage bufferImg =null;   
  19.             BufferedImage bufferImg1 = null;   
  20.             try{   
  21.                  
  22.           //先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray   
  23.           ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();   
  24.           ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();   
  25.           bufferImg = ImageIO.read(new File("d:/PieChart.jpg"));   
  26.           bufferImg1 = ImageIO.read(new File("d:/fruitBarChart.jpg"));   
  27.           ImageIO.write(bufferImg,"jpg",byteArrayOut);   
  28.           ImageIO.write(bufferImg1,"jpg",byteArrayOut1);   
  29.            
  30.         //创建一个工作薄   
  31.        HSSFWorkbook wb = new HSSFWorkbook();   
  32.        HSSFSheet sheet1 = wb.createSheet("new sheet");   
  33.        //HSSFRow row = sheet1.createRow(2);   
  34.        HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();   
  35.        HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) 1,1,(short)10,20);   
  36.        HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,512,255,(short) 2,30,(short)10,60);   
  37.        anchor1.setAnchorType(2);   
  38.        //插入图片   
  39.        patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));   
  40.        patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));   
  41.          
  42.            fileOut = new FileOutputStream("d:/workbook.xls");   
  43.            //写入excel文件   
  44.            wb.write(fileOut);   
  45.            fileOut.close();   
  46.          
  47.             }catch(IOException io){   
  48.                     io.printStackTrace();   
  49.                     System.out.println("io erorr : "+ io.getMessage());   
  50.             } finally  
  51.             {   
  52.                if (fileOut != null)   
  53.                {   
  54.                             
  55.                    try {   
  56.                               fileOut.close();   
  57.                          }   
  58.                    catch (IOException e)   
  59.                    {   
  60.                             // TODO Auto-generated catch block   
  61.                             e.printStackTrace();   
  62.                      }   
  63.                }   
  64.             }   
  65.     }   
  66. }  

Java向Excel中插入图片

标签:

原文地址:http://www.cnblogs.com/wyf19891208/p/4872219.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!