日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java poi 在excel中插入图片

發布時間:2024/1/1 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java poi 在excel中插入图片 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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



  • import java.awt.image.BufferedImage; ?
  • import java.io.ByteArrayOutputStream; ?
  • import java.io.File; ?
  • import java.io.FileOutputStream; ?
  • 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 ExcelExport { ?
  • ?
  • ? ? public static void main(String[] args) { ?
  • ? ? ? ? FileOutputStream fileOut = null;? ? ?
  • ? ? ? ? BufferedImage bufferImg = null;? ? ?
  • ? ? ? ? try { ?
  • ? ? ? ? ? ? ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); ?
  • ? ? ? ? ? //加載圖片 ?
  • ? ? ? ? ? ? bufferImg = ImageIO.read(new File("e:/1.jpg"));? ? ?
  • ? ? ? ? ? ? ImageIO.write(bufferImg, "jpg", byteArrayOut); ?
  • ? ? ? ? ? ? HSSFWorkbook wb = new HSSFWorkbook();? ? ?
  • ? ? ? ? ? ? HSSFSheet sheet1 = wb.createSheet("sheet1"); ? ?
  • ? ? ? ? ? ? HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();? ? ?
  • ? ? ? ? ? ? /**?
  • ? ? ? ? ? ? ? ? dx1 - the x coordinate within the first cell.//定義了圖片在第一個cell內的偏移x坐標,既左上角所在cell的偏移x坐標,一般可設0?
  • ? ? ? ? ? ? ? ? dy1 - the y coordinate within the first cell.//定義了圖片在第一個cell的偏移y坐標,既左上角所在cell的偏移y坐標,一般可設0?
  • ? ? ? ? ? ? ? ? dx2 - the x coordinate within the second cell.//定義了圖片在第二個cell的偏移x坐標,既右下角所在cell的偏移x坐標,一般可設0?
  • ? ? ? ? ? ? ? ? dy2 - the y coordinate within the second cell.//定義了圖片在第二個cell的偏移y坐標,既右下角所在cell的偏移y坐標,一般可設0?
  • ? ? ? ? ? ? ? ? col1 - 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.//圖片右下角所在行?
  • ? ? ? ? ? ? */ ?
  • ? ? ? ? ? ? HSSFClientAnchor anchor = new HSSFClientAnchor(-100, 0, 0, 0,(short) 2, 2, (short) 5, 8);? ? ?
  • ? ? ? ? ? ? //插入圖片 ? ?
  • ? ? ? ? ? ? patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));? ?
  • ? ? ? ? ? ? fileOut = new FileOutputStream("e:/excel.xls");? ? ?
  • ? ? ? ? ? ? // 輸出文件? ?
  • ? ? ? ? ? ? wb.write(fileOut);? ? ?
  • ? ? ? ? } catch (Exception e) { ?
  • ? ? ? ? ? ? e.printStackTrace(); ?
  • ? ? ? ? } ?
  • ? ? } ?
  • } ?

  • 關于dx1的設置的說明,dx2dy1等都是類似的


    關于一個excel設置設置多張圖片的demo

    [java] view plain copy



  • package com.poi; ?
  • ?
  • import java.awt.image.BufferedImage; ?
  • import java.io.ByteArrayOutputStream; ?
  • import java.io.File; ?
  • import java.io.FileOutputStream; ?
  • 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 ExcelExport { ?
  • ?
  • ? ? public static void main(String[] args) { ?
  • ? ? ? ? FileOutputStream fileOut = null;? ? ?
  • ? ? ? ? BufferedImage bufferImg = null;? ? ?
  • ? ? ? ? try { ?
  • ? ? ? ? ? ? ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); ?
  • ? ? ? ? ? //加載圖片 ?
  • ? ? ? ? ? ? bufferImg = ImageIO.read(new File("e:/1.jpg"));? ? ?
  • ? ? ? ? ? ? ImageIO.write(bufferImg, "jpg", byteArrayOut); ?
  • ? ? ? ? ? ? HSSFWorkbook wb = new HSSFWorkbook();? ? ?
  • ? ? ? ? ? ? HSSFSheet sheet1 = wb.createSheet("sheet1"); ? ?
  • ? ? ? ? ? ? HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();? ? ?
  • ? ? ? ? ? ? HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,(short) 2, 2, (short) 5, 8); ?
  • ? ? ? ? ? ? //插入圖片 1? ?
  • ? ? ? ? ? ? patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); ?
  • ? ? ? ? ? ? ?
  • ? ? ? ? ? ? //圖片2 ?
  • ? ? ? ? ? ? anchor = new HSSFClientAnchor(200, 0, 0, 0,(short) 2, 9, (short) 5, 15); ?
  • ? ? ? ? ? ? patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); ?
  • ? ? ? ? ? ? fileOut = new FileOutputStream("e:/excel.xls");? ? ?
  • ? ? ? ? ? ? // 輸出文件? ?
  • ? ? ? ? ? ? wb.write(fileOut);? ? ?
  • ? ? ? ? } catch (Exception e) { ?
  • ? ? ? ? ? ? e.printStackTrace(); ?
  • ? ? ? ? } ?
  • ? ? } ?
  • } ?
  • 總體來說使用poi還是很方便的。


    總結

    以上是生活随笔為你收集整理的java poi 在excel中插入图片的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。