Java将png、jpg、bmp等格式图片批量插入到Excel单个单元格内

Java将png、jpg、bmp等格式图片批量插入到Excel单个单元格内

最近做一个项目,需将多个图片(比如:jpg、bmp、png等图片格式)按实际大小插入同一个单元格,现把它记录下来,供大家参考。

由于本人程序员猿一枚,而且处在初期堆砌代码阶段,如果程序有错误的地方,真诚希望大家多多指点;如果对大家有所帮助,希望给个赞鼓励一下。具体如下:

前期准备:需要下载jxl.jar包

主要有两个类,imgFile和ExcelPicture

imgFile类,主要包括ByteArrayOutputStream pngByteArray,double width,double heigth三个属性

[html] view plain copy
  1. import java.io.ByteArrayOutputStream;    
  2. public class ImgFile {    
  3.       private ByteArrayOutputStream pngByteArray;//  
  4.     private double width;    
  5.     private double heigth;    
  6.    
  7.     public double getWidth() {    
  8.         
  9.         return width;    
  10.     }    
  11.     public void setWidth(double width) {    
  12.         
  13.         this.width = width;    
  14.     }    
  15.     public double getHeigth() {    
  16.         
  17.         return heigth;    
  18.     }    
  19.     public void setHeigth(double heigth) {    
  20.         
  21.         this.heigth = heigth;    
  22.     }  
  23.       public ByteArrayOutputStream getPngByteArray() {  
  24.             return pngByteArray;  
  25.       }  
  26.       public void setPngByteArray(ByteArrayOutputStream outPut) {  
  27.            this.pngByteArray = outPut;  
  28.       }  
  29. }  
[html] view plain copy
  1.   

ExcelPicture类代码如下

[java] view plain copy
  1. import java.awt.image.BufferedImage;  
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.File;  
  4.   
  5. import javax.imageio.ImageIO;  
  6.   
  7. import jxl.Workbook;  
  8. import jxl.format.Alignment;  
  9. import jxl.format.Border;  
  10. import jxl.format.BorderLineStyle;  
  11. import jxl.format.Colour;  
  12. import jxl.format.UnderlineStyle;  
  13. import jxl.format.VerticalAlignment;  
  14. import jxl.write.Label;  
  15. import jxl.write.WritableCellFormat;  
  16. import jxl.write.WritableFont;  
  17. import jxl.write.WritableImage;  
  18. import jxl.write.WritableSheet;  
  19. import jxl.write.WritableWorkbook;  
  20.   
  21. /** 
  22. * @ClassName: ExcelPicture 
  23. * @Description: TODO(将多种格式的图片插入到excel 一个单元格内) 
  24. * @date 2017年9月21日 下午4:21:11 
  25. * 
  26. */  
  27. public class ExcelPicture {  
  28.      public static void main(String[] args) throws Exception {      
  29.                 System.out.println("开始插入图片");  
  30.         //创建Excel工作簿;      
  31.         WritableWorkbook workbook = Workbook.createWorkbook(new File("F:/InsertPictureToExcel.xls"));      
  32.         //创建Excel电子薄;      
  33.         WritableSheet sheet = workbook.createSheet("插入图片演示"0);       
  34.         //图片路径    
  35.         String[] filePaths = new String[4];    
  36.         filePaths[0] = "F:\\2.bmp";    
  37.         filePaths[1] = "F:\\2.jpg";    
  38.         filePaths[2] = "F:\\2.png";  
  39.         filePaths[3] = "F:\\2.gif";  
  40.         //调用图片插入函数    
  41.         addPictureToExcel(sheet,filePaths,3,3);    
  42.         //写入Excel表格中;      
  43.         workbook.write();      
  44.         //关闭流;      
  45.         workbook.close();      
  46.         System.out.println("恭喜,图片插入成功!");    
  47.     }      
  48.         
  49.     /**   
  50.      *    
  51.     * @Title: addPictureToExcel   
  52.     * @Description: TODO(将多个图片按实际大小,插入同一个单元格,最后一张图如果高度超过了单元格,则压缩高度使之在单元格内)   
  53.     * @date 2016年12月16日 下午6:13:52   
  54.     * @param @param picSheet   
  55.     * @param @param pictureFilePaths   
  56.     * @param @param cellRow   
  57.     * @param @param cellCol   
  58.     * @param @throws Exception 设定文件   
  59.     * @return void 返回类型   
  60.     * @throws   
  61.      */    
  62.     private static void addPictureToExcel(WritableSheet picSheet, String[] pictureFilePaths, double cellRow, double cellCol)      
  63.             throws Exception {    
  64.             
  65.         final double cellSpace = 0.02;//图片之间的间隔 占比    
  66.             
  67.         double picWidthMax = 0;    
  68.         double picHeightSum =0;//空出图片 离上下边框的距离    
  69.         ImgFile[] imgFiles = new ImgFile[pictureFilePaths.length];    
  70.             
  71.         for (int i=0;i<pictureFilePaths.length;i++) {    
  72.             ImgFile imgFile = new ImgFile();    
  73.             File imageFile = new File(pictureFilePaths[i]);     
  74.             // 读入图片     
  75.             BufferedImage picImage = ImageIO.read(imageFile);  
  76.             ByteArrayOutputStream pngByteArray = new ByteArrayOutputStream();  
  77.             //将其他图片格式写成png的形式  
  78.             ImageIO.write(picImage, "PNG", pngByteArray);  
  79.             imgFile.setPngByteArray(pngByteArray);  
  80.             // 取得图片的像素高度,宽度      
  81.             double picWidth = picImage.getWidth() * 0.15;  //具体的实验值,原理不清楚。      
  82.             double picHeight = picImage.getHeight() * 15//具体的实验值,原理不清楚。      
  83.                 
  84.             imgFile.setHeigth(picHeight);    
  85.             imgFile.setWidth(picWidth);    
  86.             //汇总    
  87.             if (picWidth > picWidthMax) {    
  88.                 picWidthMax = picWidth;    
  89.             }    
  90.             picHeightSum += picHeight;    
  91.             imgFiles[i] = imgFile;      
  92.         }    
  93.                         
  94.         WritableFont font = new WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.RED);      
  95.         WritableCellFormat cellFormat = new WritableCellFormat(font);      
  96.         //设置背景颜色;      
  97.         cellFormat.setBackground(Colour.WHITE);      
  98.         //设置边框;      
  99.         cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);      
  100.         //设置自动换行;      
  101.         cellFormat.setWrap(true);      
  102.         //设置文字居中对齐方式;      
  103.         cellFormat.setAlignment(Alignment.CENTRE);      
  104.         //设置垂直居中;      
  105.         cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);      
  106.             
  107.         Label imageLabel = new Label((int)cellCol, (int)cellRow, "",cellFormat);     
  108.         picSheet.addCell(imageLabel);    
  109.             
  110.         //设置单元格宽高    
  111.         picSheet.setColumnView((int)cellCol, (int)picWidthMax);//列宽    
  112.         picSheet.setRowView((int)cellRow, (int)picHeightSum);//行高    
  113.             
  114.         double widthStart = cellSpace;//开始宽度    
  115.         double heightStart = cellSpace;//开始高度    
  116.         //插入图片    
  117.         for (ImgFile imgFile0: imgFiles) {    
  118.             double heigthFact = imgFile0.getHeigth()/picHeightSum;//实际高度    
  119.             double widthFact = imgFile0.getWidth()/picWidthMax;    
  120.             //图片高度压缩了cellSpace+moreHeight,目的是为了该图片高度不超出单元格    
  121.             if (heightStart + heigthFact >= 1) {    
  122.                 double moreHeight = heightStart + heigthFact - 1.00;    
  123.                 heigthFact -= moreHeight;    
  124.                 heigthFact -= cellSpace;    
  125.             }    
  126.             //图片宽度压缩了cellSpace,目的是为了该图片宽度不超出单元格    
  127.             if (widthFact >= 1) {    
  128.                 widthFact -= cellSpace;    
  129.             }    
  130.             //生成图片对象    
  131.             WritableImage image = new WritableImage(cellCol+widthStart, cellRow + heightStart,      
  132.                     widthFact, heigthFact, imgFile0.getPngByteArray().toByteArray());      
  133.             //将图片对象插入到sheet      
  134.             picSheet.addImage(image);      
  135.            //开始高度累加,获取下一张图片的起始高度(相对该单元格)    
  136.             heightStart += heigthFact;    
  137.             heightStart +=cellSpace;//图片直接间隔为cellSpace    
  138.         }    
  139.     }      

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值