我根据模版导出文档,列表里有字段是多图片,我自定义的CustomImageModifyHandler实现了CellWriteHandler重写的afterCellDispose方法
触发Bug的代码 @Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead){
// 在 单元格写入完毕后 ,自己填充图片
if (isHead || CollectionUtils.isEmpty(cellDataList)) {
return;
}
boolean listFlag = false;
Sheet sheet = cell.getSheet();
// 此处为ListUrlConverterUtil的返回值
List<ImageData> imageDataList = cellDataList.get(0).getImageDataList();
if (CollectionUtils.isNotEmpty(imageDataList)){
listFlag = true;
}
if (!listFlag && imageDataList == null){
return;
}
String key = cell.getRowIndex() + "_" + cell.getColumnIndex();
if (repeats.contains(key)){
return;
}
repeats.add(key);
if (imageDataList.size() > maxDataSize) {
maxDataSize = imageDataList.size();
}
// 默认要导出的图片大小为60*60px,60px的行高大约是900,60px列宽大概是248*8
sheet.getRow(cell.getRowIndex()).setHeight((short)900);
sheet.setColumnWidth(cell.getColumnIndex(),listFlag?240*8*imageDataList.size():240*8);
if (imageDataList.size() > maxDataSize) {
maxDataSize = imageDataList.size();
}
//图片列最大图片数
AtomicReference<Integer> maxImageSize = new AtomicReference<>(0);
maxImageSize.set(imageDataList.size());
//每张图片间距
int splitWidth = 2;
//每张图片的长度
int imageWidth = 80;
//图片列的最大长度
int sumWidth = maxImageSize.get() * (imageWidth + splitWidth);
if (listFlag){
for (int i = 0; i < imageDataList.size(); i++) {
int left = imageWidth * (i - 1) + i * splitWidth;
int right = sumWidth - imageWidth - left;
ImageData imageData = imageDataList.get(i);
if(imageData ==null){
continue;
}
imageData.setTop(1);
//距离单元格底部距离
imageData.setBottom(1);
//距离单元格左边距离
imageData.setLeft(left);
//距离单元格右边距离
imageData.setRight(right);
byte[] image = imageData.getImage();
this.insertImage(sheet,cell, image,i);
}
}else {
this.insertImage(sheet,cell, imageDataList.get(0).getImage(),0);
}
}
private void insertImage(Sheet sheet, Cell cell, byte[] pictureData, int i){
int picWidth = Units.pixelToEMU(60);
int index = sheet.getWorkbook().addPicture(pictureData, HSSFWorkbook.PICTURE_TYPE_PNG);
Drawing<?> drawing = sheet.getDrawingPatriarch();
if (drawing == null) {
drawing = sheet.createDrawingPatriarch();
}
CreationHelper helper = sheet.getWorkbook().getCreationHelper();
ClientAnchor anchor = helper.createClientAnchor();
// 设置图片坐标
anchor.setDx1(picWidth*i);
anchor.setDx2(picWidth+picWidth*i);
anchor.setDy1(0);
anchor.setDy2(0);
//设置图片位置
int columnIndex = cell.getColumnIndex();
anchor.setCol1(columnIndex);
anchor.setCol2(columnIndex);
int rowIndex = cell.getRowIndex();
anchor.setRow1(rowIndex);
anchor.setRow2(rowIndex + 1);
anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
drawing.createPicture(anchor, index);
}
提示的异常或者没有达到的效果
这是导出后看到的效果 把上面的10张图拿开以后 下面重叠着10张填充单元格的图片
大家尽量把问题一次性描述清楚,然后贴上全部异常,这样方便把问题一次性解决掉。 至少大家要符合一个原则就是,能让其他人复现出这个问题,如果无法复现,肯定无法解决。