最終效果
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-596019.html
?pom版本
<developer>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</developer>
實(shí)現(xiàn)代碼
package com.alibaba.easyexcel.test.demo.write;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.data.ClientAnchorData;
import com.alibaba.excel.metadata.data.ImageData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.write.style.column.AutoColumnWidthStyleStrategy;
import org.apache.commons.collections4.CollectionUtils;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
/**
* 圖片導(dǎo)出
*
* @author dqh
* @date 2022/11/3 15:20
*/
public class ImageExportTest {
/**
* 圖片導(dǎo)出
* <p>
* 1. 創(chuàng)建excel對(duì)應(yīng)的實(shí)體對(duì)象 參照{(diào)@link ImageDemoData}
* <p>
* 2. 直接寫即可
*/
@Test
public void imageWrite() throws Exception {
//文件最后生成的路徑
String fileName = TestFileUtil.getPath() + "imageWrite.xlsx";
System.out.println(fileName);
//測(cè)試用的圖片路徑
String imagePath = TestFileUtil.getPath() + "converter" + File.separator + "img.jpg";
String imagePath2 = TestFileUtil.getPath() + "converter" + File.separator + "img2.jpeg";
//數(shù)據(jù)列表Demo
List<ImageDemoData> demoDataList = new ArrayList<>();
//圖片路徑
List<String> path1 = new ArrayList<>();
path1.add(imagePath);
List<String> path2 = new ArrayList<>();
path2.add(imagePath);
path2.add(imagePath2);
List<String> path3 = new ArrayList<>();
path3.add(imagePath);
path3.add(imagePath);
path3.add(imagePath2);
List<String> path4 = new ArrayList<>();
path4.add(imagePath);
path4.add(imagePath2);
path4.add(imagePath);
path4.add(imagePath2);
demoDataList.add(new ImageDemoData("王離", path1));
demoDataList.add(new ImageDemoData("楊開(kāi)", path2));
demoDataList.add(new ImageDemoData("歐陽(yáng)微微", path3));
demoDataList.add(new ImageDemoData("上官婉兒", path4));
genImageExcel(demoDataList, fileName);
}
/**
* 生成有圖片的excel
*
* @param demoDataList 數(shù)據(jù)列表
* @param fileName 生成文件路徑
*/
private void genImageExcel(List<ImageDemoData> demoDataList, String fileName) {
if (CollectionUtils.isEmpty(demoDataList)) {
return;
}
//圖片列最大圖片數(shù)
AtomicReference<Integer> maxImageSize = new AtomicReference<>(0);
demoDataList.forEach(item -> {
if (CollectionUtils.isNotEmpty(item.getImagePathList()) && item.getImagePathList().size() > maxImageSize.get()) {
maxImageSize.set(item.getImagePathList().size());
}
});
//設(shè)置列長(zhǎng)度所用類
AutoColumnWidthStyleStrategy longWidth = new AutoColumnWidthStyleStrategy();
demoDataList.forEach(item -> {
WriteCellData<Void> writeCellData = new WriteCellData<>();
if (CollectionUtils.isNotEmpty(item.getImagePathList())) {
//每張圖片間距
Integer splitWidth = 2;
//每張圖片的長(zhǎng)度
Integer imageWidth = 80;
//圖片列的最大長(zhǎng)度
Integer sumWidth = maxImageSize.get() * (imageWidth + splitWidth);
List<ImageData> imageDataList = new ArrayList<>();
List<String> imagePathList = item.getImagePathList();
for (int i = 1; i <= imagePathList.size(); i++) {
String path = imagePathList.get(i - 1);
Integer left = imageWidth * (i - 1) + i * splitWidth;
Integer right = sumWidth - imageWidth - left;
ImageData imageData = new ImageData();
try {
imageData.setImage(FileUtils.readFileToByteArray(new File(path)));
} catch (IOException e) {
e.printStackTrace();
}
imageData.setImageType(ImageData.ImageType.PICTURE_TYPE_PNG);
//距離單元格頂部距離
imageData.setTop(1);
//距離單元格底部距離
imageData.setBottom(1);
//距離單元格左邊距離
imageData.setLeft(left);
//距離單元格右邊距離
imageData.setRight(right);
imageData.setAnchorType(ClientAnchorData.AnchorType.DONT_MOVE_DO_RESIZE);
imageDataList.add(imageData);
}
writeCellData.setImageDataList(imageDataList);
Map<String, Integer> zdyColumnWidth = new HashMap<>();
//圖片列名稱,對(duì)應(yīng)導(dǎo)出對(duì)象的列名稱,圖片列長(zhǎng)度
zdyColumnWidth.put("上傳圖片", sumWidth / 6);
longWidth.setZdyColumnWidth(zdyColumnWidth);
}
item.setWriteCellDataFile(writeCellData);
});
//寫入數(shù)據(jù)
EasyExcel.write(fileName, ImageDemoData.class).registerWriteHandler(longWidth).sheet().doWrite(demoDataList);
}
}
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-596019.html
package com.alibaba.easyexcel.test.demo.write;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.metadata.data.WriteCellData;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
/**
* 圖片導(dǎo)出類
*
* @author Jiaju Zhuang
*/
@Getter
@Setter
@EqualsAndHashCode
@ContentRowHeight(40)
public class ImageDemoData {
/**
* 根據(jù)文件導(dǎo)出 并設(shè)置導(dǎo)出的位置。
*
* @since 3.0.0-beta1
*/
@ExcelProperty(value = "用戶名稱")
private String test;
/**
* 圖片路徑
*/
@ExcelIgnore
private List<String> imagePathList;
/**
* 圖片列
*/
@ExcelProperty(value = "上傳圖片")
private WriteCellData<Void> writeCellDataFile;
public ImageDemoData() {
}
public ImageDemoData(String test, List<String> imagePathList) {
this.test = test;
this.imagePathList = imagePathList;
}
public ImageDemoData(List<String> imagePathList) {
this.imagePathList = imagePathList;
}
}
package com.alibaba.excel.write.style.column;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.MapUtils;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 自動(dòng)列寬
*/
public class AutoColumnWidthStyleStrategy extends AbstractColumnWidthStyleStrategy {
private static final int MAX_COLUMN_WIDTH = 255;
//自定義列的列寬
private Map<String, Integer> zdyColumnWidth = MapUtils.newHashMapWithExpectedSize(2);
private final Map<Integer, Map<Integer, Integer>> cache = MapUtils.newHashMapWithExpectedSize(8);
@Override
protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell,
Head head,
Integer relativeRowIndex, Boolean isHead) {
boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList);
if (!needSetWidth) {
return;
}
if (zdyColumnWidth.containsKey(cell.toString())) {
writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), zdyColumnWidth.get(cell.toString()) * 256);
return;
}
Map<Integer, Integer> maxColumnWidthMap = cache.get(writeSheetHolder.getSheetNo());
if (maxColumnWidthMap == null) {
maxColumnWidthMap = new HashMap<Integer, Integer>(16);
cache.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap);
}
Integer columnWidth = dataLength(cellDataList, cell, isHead);
if (columnWidth < 0) {
return;
}
if (columnWidth > MAX_COLUMN_WIDTH) {
columnWidth = MAX_COLUMN_WIDTH;
}
Integer maxColumnWidth = maxColumnWidthMap.get(cell.getColumnIndex());
if (maxColumnWidth == null || columnWidth > maxColumnWidth) {
maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth);
writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 256);
}
}
private Integer dataLength(List<WriteCellData<?>> cellDataList, Cell cell, Boolean isHead) {
if (isHead) {
return cell.getStringCellValue().getBytes().length;
}
WriteCellData<?> cellData = cellDataList.get(0);
CellDataTypeEnum type = cellData.getType();
if (type == null) {
return -1;
}
switch (type) {
case STRING:
return cellData.getStringValue().getBytes().length;
case BOOLEAN:
return cellData.getBooleanValue().toString().getBytes().length;
case NUMBER:
return cellData.getNumberValue().toString().getBytes().length;
default:
return -1;
}
}
public void setZdyColumnWidth(Map<String, Integer> zdyColumnWidth) {
this.zdyColumnWidth = zdyColumnWidth;
}
}
到了這里,關(guān)于【JAVA】easyexcel 導(dǎo)出excel文件帶多個(gè)圖片的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!