excel一个单元格中导入多张图片的案例
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
excel⼀个单元格中导⼊多张图⽚的案例
导⼊依赖
新建两个实体类
实体类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
}
}
}。