首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

两种形式向Excel中插入图片

2012-10-25 
两种方式向Excel中插入图片????? 目前对 Excel 的各种操作主要通过 POI 或 jxl? 实现.? 下面分别简要介绍

两种方式向Excel中插入图片

????? 目前对 Excel 的各种操作主要通过 POI 或 jxl? 实现.? 下面分别简要介绍如何向 Excel 中插入图片.

?

????? 1、利用 POI 向Excel中插入图片

?????? 可参考:http://gocom.primeton.com/blog10751_1221.htm

??? ?? 测试代码:

?

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("C:/graph.jpg"));
??? ??? ??? bufferImg1 = ImageIO.read(new File("c:/person.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, 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("C:/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、利用 jxl(jexcelapi) 向Excel中插入图片

??? ?? 可参考:http://hi.baidu.com/82826200/blog/item/4c380f239c426c569922ed33.html

?????? 目前, 此方式只支持 PNG 格式图片.

?????? 测试代码:

String path = "c:/testJxl.xls";
??? ??? File file = new File(path);
??? ??? if (file.exists())
??? ??? ??? file.delete();

??? ??? boolean flag = true;
??? ??? WritableWorkbook wwb = null;
??? ??? try {
??? ??? ??? wwb = Workbook.createWorkbook(file);
??? ??? ??? WritableSheet ws = wwb.createSheet("Test Sheet 1", 0);
??? ??? ??? File fileImage = new File("C:\\graph.png");
??? ??? ???
??? ??? ??? BufferedImage bufImage = ImageIO.read(fileImage);
??? ??? ??? double height = bufImage.getHeight();
??? ??? ??? double width = bufImage.getWidth();
??? ??? ???
??? ??? ??? WritableImage image = new WritableImage(1, 4, 6, 18, fileImage);?????????
??? ??? ???
??? ??? ??? ws.addImage(image);
??? ??? ??? wwb.write();
??? ??? ??? try {
??? ??? ??? ??? wwb.close();
??? ??? ??? } catch (WriteException e) {
??? ??? ??? ??? flag = false;
??? ??? ??? } catch (IOException e) {
??? ??? ??? ??? flag = false;
??? ??? ??? }
??? ??? } catch (IOException e) {
??? ??? ??? flag = false;
??? ??? }

??? ??? if (flag)
??? ??? ??? System.out.println("Success!");
??? ??? else
??? ??? ??? System.out.println("False!");
??? }

?

附:

???? POI 下载地址:http://apache.mirror.phpchina.com/poi/

???? jxl 下载地址:? http://sourceforge.net/project/showfiles.php?group_id=79926

?

热点排行