excel一个单元格中导入多张图片的案例


导入依赖

<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>

新建两个实体类

实体类1
import lombok.Data;

import java.io.ByteArrayOutputStream;

@Data
public class ImgFile {
private ByteArrayOutputStream pngByteArray;
private double width;
private double heigth;

}

 

 

实体类2

import jxl.Workbook;
import jxl.format.*;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;

public class ExcelPicture {

public static void picture() throws Exception {
System.out.println("开始插入图片");
//创建Excel工作簿;
WritableWorkbook workbook = Workbook.createWorkbook(new File("D:\\test\\InsertPictureToExcel.xls"));
//创建Excel电子薄;
WritableSheet sheet = workbook.createSheet("插入图片演示", 0);
//图片路径
String[] filePaths = new String[4];
filePaths[0] = "D:\\picture\\aa.jpg";
filePaths[1] = "D:\\picture\\bb.jpg";
filePaths[2] = "D:\\picture\\cc.jpg";
filePaths[3] = "D:\\picture\\dd.jpg";
//调用图片插入函数
addPictureToExcel(sheet,filePaths,3,3);
//写入Excel表格中;
workbook.write();
//关闭流;
workbook.close();
System.out.println("恭喜,图片插入成功!");
}

/**
*
* @Title: addPictureToExcel
* @Description: TODO(将多个图片按实际大小,插入同一个单元格,最后一张图如果高度超过了单元格,则压缩高度使之在单元格内)
* @date 2016年12月16日 下午6:13:52
* @param @param picSheet
* @param @param pictureFilePaths
* @param @param cellRow
* @param @param cellCol
* @param @throws Exception 设定文件
* @return void 返回类型
* @throws
*/
private static void addPictureToExcel(WritableSheet picSheet, String[] pictureFilePaths, double cellRow, double cellCol)
throws Exception {

final double cellSpace = 0.02;//图片之间的间隔 占比

double picWidthMax = 0;
double picHeightSum =0;//空出图片 离上下边框的距离
ImgFile[] imgFiles = new ImgFile[pictureFilePaths.length];

for (int i=0;i<pictureFilePaths.length;i++) {
ImgFile imgFile = new ImgFile();
File imageFile = new File(pictureFilePaths[i]);
// 读入图片
BufferedImage picImage = ImageIO.read(imageFile);
ByteArrayOutputStream pngByteArray = new ByteArrayOutputStream();
//将其他图片格式写成JPG的形式
ImageIO.write(picImage, "JPG", pngByteArray);
imgFile.setPngByteArray(pngByteArray);
//imageFile.set
// 取得图片的像素高度,宽度
//double picWidth = picImage.getWidth() * 0.15; //具体的实验值,原理不清楚。
//double picHeight = picImage.getHeight() * 15; //具体的实验值,原理不清楚。

double picWidth = 70;
double picHeight = 100;

imgFile.setHeigth(picHeight);
imgFile.setWidth(picWidth);
//汇总
if (picWidth > picWidthMax) {
picWidthMax = picWidth;
}
picHeightSum += picHeight;
imgFiles[i] = imgFile;
}

WritableFont font = new WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.RED);
WritableCellFormat cellFormat = new WritableCellFormat(font);
//设置背景颜色;
cellFormat.setBackground(Colour.WHITE);
//设置边框;
cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
//设置自动换行;
cellFormat.setWrap(true);
//设置文字居中对齐方式;
cellFormat.setAlignment(Alignment.CENTRE);
//设置垂直居中;
cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);

Label imageLabel = new Label((int)cellCol, (int)cellRow, "",cellFormat);
picSheet.addCell(imageLabel);

//设置单元格宽高
picSheet.setColumnView((int)cellCol, (int)picWidthMax);//列宽
picSheet.setRowView((int)cellRow, (int)picHeightSum);//行高

double widthStart = cellSpace;//开始宽度
double heightStart = cellSpace;//开始高度
//插入图片
for (ImgFile imgFile0: imgFiles) {
double heigthFact = imgFile0.getHeigth()/picHeightSum;//实际高度
double widthFact = imgFile0.getWidth()/picWidthMax;
//图片高度压缩了cellSpace+moreHeight,目的是为了该图片高度不超出单元格
if (heightStart + heigthFact >= 1) {
double moreHeight = heightStart + heigthFact - 1.00;
heigthFact -= moreHeight;
heigthFact -= cellSpace;
}
//图片宽度压缩了cellSpace,目的是为了该图片宽度不超出单元格
if (widthFact >= 1) {
widthFact -= cellSpace;
}
//生成图片对象
WritableImage image = new WritableImage(cellCol+widthStart, cellRow + heightStart,
widthFact, heigthFact, imgFile0.getPngByteArray().toByteArray());
//将图片对象插入到sheet
picSheet.addImage(image);
//开始高度累加,获取下一张图片的起始高度(相对该单元格)
heightStart += heigthFact;
heightStart +=cellSpace;//图片直接间隔为cellSpace
}
}

 


}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM