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