导出图片到excel bug修复

This commit is contained in:
guoshengxiong 2025-09-25 10:32:31 +08:00
parent bc06c9daa1
commit a24cbd89ba

View File

@ -915,12 +915,21 @@ public class ExcelUtils {
int colIndex = startCol; int colIndex = startCol;
for (byte[] bytes : bytesList) { for (byte[] bytes : bytesList) {
// 跳过空图片
if (bytes == null || bytes.length == 0) { if (bytes == null || bytes.length == 0) {
// 根据方向调整位置跳过当前行或列
if (direction == 1) {
// 纵向排列向下移动一行
rowIndex++;
} else {
// 横向排列向右移动一列
colIndex++;
}
continue; continue;
} }
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) { try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {
// 获取图片格式简单判断实际可能需要更复杂的检测 // 获取图片格式
int pictureType = getPictureType(bytes); int pictureType = getPictureType(bytes);
// 添加图片到工作簿 // 添加图片到工作簿
@ -955,7 +964,13 @@ public class ExcelUtils {
} }
} catch (Exception e) { } catch (Exception e) {
log.error("", e); log.error("导出图片失败", e);
// 发生异常时也继续处理下一个图片但记录错误
if (direction == 1) {
rowIndex++;
} else {
colIndex++;
}
} }
} }
} }
@ -964,7 +979,7 @@ public class ExcelUtils {
* 根据图片字节数组判断图片类型 * 根据图片字节数组判断图片类型
*/ */
private static int getPictureType(byte[] bytes) { private static int getPictureType(byte[] bytes) {
if (bytes.length < 4) { if (bytes == null || bytes.length < 4) {
return Workbook.PICTURE_TYPE_PNG; return Workbook.PICTURE_TYPE_PNG;
} }