`
gybmike
  • 浏览: 180168 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

使用POI向Excel中插入多張图片

阅读更多
POI3.0以上版本才支持向Excel中插入图片

http://apache.freelamp.com/jakarta/poi/release/src/poi-src-3.0-FINAL-20070503.zip

http://apache.freelamp.com/jakarta/poi/release/bin/poi-bin-3.0-FINAL-20070503.zip

新版的API可能支持的內容更豐富,不過文件也越來越大了.


package com.emis.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 test {

    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:\\wwwroot\\smepos3.0\\images\\link01.jpg"));
          bufferImg1 = ImageIO.read(new File("D:\\wwwroot\\smepos3.0\\images\\link02.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,100,50,(short) 1,1,(short)10,20);
       HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,100,50,(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();
                     }
               }
            }
    }
}
分享到:
评论
2 楼 u010100704 2014-09-01  
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/codec/digest/DigestUtils
at org.apache.poi.hssf.usermodel.HSSFWorkbook.addPicture(HSSFWorkbook.java:1575)
at com.entity.demo12.main(demo12.java:39)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.codec.digest.DigestUtils
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 2 more
1 楼 gybmike 2010-05-26  
public HSSFClientAnchor(int dx1,
int dy1,
int dx2,
int dy2,
short col1,
int row1,
short col2,
int row2);Creates a new client anchor and sets the top-left and bottom-right coordinates of the anchor.

Parameters:
dx1 - the x coordinate within the first cell.
dy1 - the y coordinate within the first cell.
dx2 - the x coordinate within the second cell.
dy2 - the y coordinate within the second cell.
col1 - the column (0 based); of the first cell.
row1 - the row (0 based); of the first cell.
col2 - the column (0 based); of the second cell.
row2 - the row (0 based); of the second cell.



col1 图片的左上角放在第几个列cell,
row1 图片的左上角放在第几个行cell,

col2 图片的右下角放在第几个列cell,
row2 图片的右下角放在第几个行cell,

列宽
sheet.setColumnWidth((short)column,(short)width);
行高
row.setHeight((short)height);
添加多个图片时:多个pic应该share同一个DrawingPatriarch在同一个sheet里面。

相关推荐

Global site tag (gtag.js) - Google Analytics