生活随笔
收集整理的這篇文章主要介紹了
java poi 在excel中插入图片
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
java web 中導(dǎo)出 excel 數(shù)據(jù)是常見的功能,最近遇到一個需求是在 excel 中插入圖片。處理 excel 及其他微軟辦公系列軟件常用的就是 apache poi ,它也是支持圖片插入的。插入圖片最主要的用到 HSSFClientAnchor , HSSFClientAnchor 的文檔介紹如下:
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內(nèi)的偏移 x坐標(biāo),既左上角所在 cell的偏移 x坐標(biāo),一般可設(shè) 0dy1 - the y coordinate within the first cell.//定義了圖片在第一個 cell的偏移 y坐標(biāo),既左上角所在 cell的偏移 y坐標(biāo),一般可設(shè) 0dx2 - the x coordinate within the second cell.//定義了圖片在第二個 cell的偏移 x坐標(biāo),既右下角所在 cell的偏移 x坐標(biāo),一般可設(shè) 0dy2 - the y coordinate within the second cell.//定義了圖片在第二個 cell的偏移 y坐標(biāo),既右下角所在 cell的偏移 y坐標(biāo),一般可設(shè) 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 內(nèi)的偏移 x 坐標(biāo),既左上角所在 cell 的偏移 x 坐標(biāo),一般可設(shè) 0 ? ? ? ? ? ? ? ? ? dy1 - the y coordinate within the first cell.// 定義了圖片在第一個 cell 的偏移 y 坐標(biāo),既左上角所在 cell 的偏移 y 坐標(biāo),一般可設(shè) 0 ? ? ? ? ? ? ? ? ? dx2 - the x coordinate within the second cell.// 定義了圖片在第二個 cell 的偏移 x 坐標(biāo),既右下角所在 cell 的偏移 x 坐標(biāo),一般可設(shè) 0 ? ? ? ? ? ? ? ? ? dy2 - the y coordinate within the second cell.// 定義了圖片在第二個 cell 的偏移 y 坐標(biāo),既右下角所在 cell 的偏移 y 坐標(biāo),一般可設(shè) 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(); ? ? ? ? ? } ? ? ? } ? } ?
關(guān)于dx1 的設(shè)置的說明,dx2 ,dy1 等都是類似的
關(guān)于一個excel 設(shè)置設(shè)置多張圖片的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 還是很方便的。
總結(jié)
以上是生活随笔 為你收集整理的java poi 在excel中插入图片 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。