java导出excel插入图片

                       

首项需要的jar包有:

1.poi-3.8-20120326.jar2.commons-codec-1.10.jar
   
   
  • 1
  • 2
代码:    package com.demo;            import java.awt.image.BufferedImage;      import java.io.ByteArrayOutputStream;      import java.io.File;      import java.io.FileOutputStream;      import java.io.IOException;            import javax.imageio.ImageIO;            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 POI {                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();                                    //将两张图片读到BufferedImage                  bufferImg = ImageIO.read(new File("C:/person.png"));                  bufferImg1 = ImageIO.read(new File("C:/person.png"));                  ImageIO.write(bufferImg, "png", byteArrayOut);                  ImageIO.write(bufferImg1, "png", byteArrayOut1);                        // 创建一个工作薄                  HSSFWorkbook wb = new HSSFWorkbook();                  //创建一个sheet                  HSSFSheet sheet = wb.createSheet("out put excel");                        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();                  /**                  * 该构造函数有8个参数                  * 前四个参数是控制图片在单元格的位置,分别是图片距离单元格left,top,right,bottom的像素距离                  * 后四个参数,前连个表示图片左上角所在的cellNum和 rowNum,后天个参数对应的表示图片右下角所在的cellNum和 rowNum,                  * excel中的cellNum和rowNum的index都是从0开始的                  *                   */                  //图片一导出到单元格B2中                  HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,                          (short) , 1, (short) 2, 2);                  //图片二导出到单元格C3到E5中,且图片的left和top距离边框50                  HSSFClientAnchor anchor1 = new HSSFClientAnchor(50, 50, 0, 0,                          (short) 2, 2, (short) 5, 5);                        // 插入图片                  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:/output_Excel.xls");                  // 写入excel文件                  wb.write(fileOut);              } catch (IOException io) {                  io.printStackTrace();                  System.out.println("io erorr : " + io.getMessage());              } finally {                  if (fileOut != null) {                      try {                          fileOut.close();                      } catch (IOException e) {                          e.printStackTrace();                      }                  }              }          }      }  
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

这是最基本的向Excel插入图片,下一篇将会讲到项目中用到的数据和图片一起导出

           

猜你喜欢

转载自blog.csdn.net/qq_44949818/article/details/89471702