poi 生成excel 圖片
在使用poi導出報表的時候,通常會在excel中生成統計圖片,下面通過一個簡單的導出excel圖片。
package com.xolt; import java.io.FileOutputStream; import java.io.File; import java.io.ByteArrayOutputStream; import java.io.IOException;import java.awt.image.BufferedImage; import javax.imageio.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFPatriarch; import org.apache.poi.hssf.usermodel.HSSFClientAnchor;;
public class TestPOI {
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:/Documents and Settings/dingqi/Desktop/clip_image002.jpg")); bufferImg1 = ImageIO.read(new File("C:/Documents and Settings/dingqi/Desktop/clip_image002.jpg")); ImageIO.write(bufferImg,"jpg",byteArrayOut); ImageIO.write(bufferImg1,"jpg",byteArrayOut1); //創建一個工作薄 HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet1 = wb.createSheet("poi picT"); //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(); } } } }
}</pre>
本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!