java poi 在excel中插入图片

7 篇文章 0 订阅

java web中导出excel数据是常见的功能,最近遇到一个需求是在excel中插入图片。处理excel及其他微软办公系列软件常用的就是apache poi,它也是支持图片插入的。插入图片最主要的用到HSSFClientAnchorHSSFClientAnchor的文档介绍如下:

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. Note: Microsoft Excel seems to sometimes disallow higher y1 than y2 or higher x1 than x2, you might need to reverse them and draw shapes vertically or horizontally flipped!

Parameters:

dx1 - the x coordinate within the first cell.//定义了图片在第一个cell内的偏移x坐标,既左上角所在cell的偏移x坐标,一般可设0dy1 - the y coordinate within the first cell.//定义了图片在第一个cell的偏移y坐标,既左上角所在cell的偏移y坐标,一般可设0dx2 - the x coordinate within the second cell.//定义了图片在第二个cell的偏移x坐标,既右下角所在cell的偏移x坐标,一般可设0dy2 - the y coordinate within the second cell.//定义了图片在第二个cell的偏移y坐标,既右下角所在cell的偏移y坐标,一般可设0col1 - the column (0 based) of the first cell.//第一个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.//图片右下角所在行

具体demo 如下:

[java] view plain copy



  1. import java.awt.image.BufferedImage;  
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import javax.imageio.ImageIO;  
  6.  
  7. import org.apache.poi.hssf.usermodel.HSSFClientAnchor;  
  8. import org.apache.poi.hssf.usermodel.HSSFPatriarch;  
  9. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  10. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  11.  
  12. public class ExcelExport {  
  13.  
  14.     public static void main(String[] args) {  
  15.         FileOutputStream fileOut = null;     
  16.         BufferedImage bufferImg = null;     
  17.         try {  
  18.             ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();  
  19.           //加载图片  
  20.             bufferImg = ImageIO.read(new File("e:/1.jpg"));     
  21.             ImageIO.write(bufferImg, "jpg", byteArrayOut);  
  22.             HSSFWorkbook wb = new HSSFWorkbook();     
  23.             HSSFSheet sheet1 = wb.createSheet("sheet1");    
  24.             HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();     
  25.             /** 
  26.                 dx1 - the x coordinate within the first cell.//定义了图片在第一个cell内的偏移x坐标,既左上角所在cell的偏移x坐标,一般可设0 
  27.                 dy1 - the y coordinate within the first cell.//定义了图片在第一个cell的偏移y坐标,既左上角所在cell的偏移y坐标,一般可设0 
  28.                 dx2 - the x coordinate within the second cell.//定义了图片在第二个cell的偏移x坐标,既右下角所在cell的偏移x坐标,一般可设0 
  29.                 dy2 - the y coordinate within the second cell.//定义了图片在第二个cell的偏移y坐标,既右下角所在cell的偏移y坐标,一般可设0 
  30.                 col1 - the column (0 based) of the first cell.//第一个cell所在列,既图片左上角所在列 
  31.                 row1 - the row (0 based) of the first cell.//图片左上角所在行 
  32.                 col2 - the column (0 based) of the second cell.//图片右下角所在列 
  33.                 row2 - the row (0 based) of the second cell.//图片右下角所在行 
  34.             */  
  35.             HSSFClientAnchor anchor = new HSSFClientAnchor(-100, 0, 0, 0,(short) 2, 2, (short) 5, 8);     
  36.             //插入图片    
  37.             patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));   
  38.             fileOut = new FileOutputStream("e:/excel.xls");     
  39.             // 输出文件   
  40.             wb.write(fileOut);     
  41.         } catch (Exception e) {  
  42.             e.printStackTrace();  
  43.         }  
  44.     }  
  45. }  


关于dx1的设置的说明,dx2dy1等都是类似的


关于一个excel设置设置多张图片的demo

[java] view plain copy



  1. package com.poi;  
  2.  
  3. import java.awt.image.BufferedImage;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileOutputStream;  
  7. import javax.imageio.ImageIO;  
  8.  
  9. import org.apache.poi.hssf.usermodel.HSSFClientAnchor;  
  10. import org.apache.poi.hssf.usermodel.HSSFPatriarch;  
  11. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  12. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  13.  
  14. public class ExcelExport {  
  15.  
  16.     public static void main(String[] args) {  
  17.         FileOutputStream fileOut = null;     
  18.         BufferedImage bufferImg = null;     
  19.         try {  
  20.             ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();  
  21.           //加载图片  
  22.             bufferImg = ImageIO.read(new File("e:/1.jpg"));     
  23.             ImageIO.write(bufferImg, "jpg", byteArrayOut);  
  24.             HSSFWorkbook wb = new HSSFWorkbook();     
  25.             HSSFSheet sheet1 = wb.createSheet("sheet1");    
  26.             HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();     
  27.             HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,(short) 2, 2, (short) 5, 8);  
  28.             //插入图片  
  29.             patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));  
  30.              
  31.             //图片2  
  32.             anchor = new HSSFClientAnchor(200, 0, 0, 0,(short) 2, 9, (short) 5, 15);  
  33.             patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));  
  34.             fileOut = new FileOutputStream("e:/excel.xls");     
  35.             // 输出文件   
  36.             wb.write(fileOut);     
  37.         } catch (Exception e) {  
  38.             e.printStackTrace();  
  39.         }  
  40.     }  
  41. }  

总体来说使用poi还是很方便的。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java使用Apache POI可以实现向Excel插入图片。下面我将介绍一下具体实现步骤: 1. 首先需要导入POI相关的jar包。可以从官网上下载最新版本的POI,然后将poi-xxx.jar、poi-ooxml-xxx.jar、poi-ooxml-schemas-xxx.jar、ooxml-lib\dom4j-xxx.jar、ooxml-lib\xmlbeans-xxx.jar这些jar包导入到项目。 2. 创建一个工作簿对象,并在其创建一个工作表对象。 ``` Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); ``` 3. 创建一个文件输入流对象,读取图片文件。 ``` InputStream inputStream = new FileInputStream("图片文件路径"); ``` 4. 将图片数据读取到一个字节数组。 ``` byte[] bytes = IOUtils.toByteArray(inputStream); ``` 5. 创建一个绘图对象,并将图片数据添加到绘图对象。 ``` Drawing<?> drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 0, 0, 1, 1); Picture pic = drawing.createPicture(anchor, workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG)); ``` 6. 将Excel文件保存到本地。 ``` FileOutputStream outputStream = new FileOutputStream("Excel文件保存路径"); workbook.write(outputStream); outputStream.close(); ``` 以上就是使用Java POIExcel插入图片的具体实现步骤。需要注意的是,这里使用的是XSSFWorkbook和XSSFClientAnchor,所以生成的Excel文件格式是xlsx。如果需要生成xls格式的文件,需要使用HSSFWorkbookHSSFClientAnchor。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值