ITPub博客

首页 > Linux操作系统 > Linux操作系统 > POI向EXCEL写图片(转)

POI向EXCEL写图片(转)

原创 Linux操作系统 作者:280511772 时间:2009-06-28 21:18:48 0 删除 编辑

package com.my.common;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelWriter {

 public static void main(String[] args) throws IOException {
  FileOutputStream ut = new FileOutputStream(
    "E:/code/MyPro/WebRoot/excel/temp/download.xls");
  HSSFWorkbook wb = new HSSFWorkbook();
  HSSFSheet sheet1 = wb.createSheet("picture");
  HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
  HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 0, 0, (short) 0,
    0, (short) 8, 20);
  patriarch.createPicture(anchor1, wb.addPicture(
    loadImage("E:/code/MyPro/WebRoot/excel/down.jpg"),
    HSSFWorkbook.PICTURE_TYPE_JPEG));
  wb.write(out);
  out.close();
 }

 public static byte[] loadImage(String filePath) {
  try {
   FileInputStream input = new FileInputStream(new File(filePath));
   ByteArrayOutputStream utput = new ByteArrayOutputStream();
   byte buf[] = new byte[1024];
   for (;;) {
    int noBytesRead = input.read(buf);
    if (noBytesRead == -1) {
     return output.toByteArray();
    }
    output.write(buf, 0, noBytesRead);
   }
  } catch (IOException e) {
   throw new RuntimeException(e);
  }
 }
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/13545951/viewspace-607765/,如需转载,请注明出处,否则将追究法律责任。

下一篇: 构造连续的序列
请登录后发表评论 登录
全部评论

注册时间:2008-09-19

  • 博文量
    23
  • 访问量
    21186