使用 LibreOffice Calc 打开文件时,使用 apache poi 在 excel 的第 3000 到 3005 行写入图像 java

Write image in excel at row 3000 to 3005 in java using apache poi when using LibreOffice Calc to open file

我正在使用 poi 3.9 创建 xlsx 文件一切正常,直到我的文件长度超过 1600 行。跨越 1600 行后,我可以写入数据,但我 无法写入图像 所有图像都在第 1640 行相互附加,这很奇怪,我很长时间以来都在研究 poi并选择了它的库限制的问题并将我的 poi 更新为 3.15 但同样的问题,在这里我可以写入多达 2000 行的图像 然后我试过 poi 3.16 但问题还是一样,但在这里我可以写图像多达 2500 行 。 下面是我写图片的代码

private void drawImageOnExcelSheet(XSSFSheet sitePhotosSheet, int row1,
        int row2, int col1, int col2, String fileName) {
    try {
        InputStream is = new FileInputStream(fileName);
        byte[] bytes = IOUtils.toByteArray(is);
        int pictureIdx = sitePhotosSheet.getWorkbook().addPicture(bytes,Workbook.PICTURE_TYPE_JPEG);
        is.close();
        CreationHelper helper = sitePhotosSheet.getWorkbook().getCreationHelper();

        Drawing drawing = sitePhotosSheet.createDrawingPatriarch();

        ClientAnchor anchor = helper.createClientAnchor();
        anchor.setAnchorType(AnchorType.MOVE_AND_RESIZE);

        anchor.setCol1(col1);
        anchor.setCol2(col2);
        anchor.setRow1(row1);
        anchor.setRow2(row2);
        drawing.createPicture(anchor, pictureIdx);
    } catch(Exception e) {
        e.printStackTrace();
}

注意:我可以写入数据,但只有图像出现问题。请建议我如何解决这个问题。 请看下面的图片,您可以看到在第 1639 行,两张图片相互附加,这两张图片后面有很多图片,因为我在控制台上打印的图片的最后一行是 3400。

使用apache poi版本3.16和Java8(我不是老软件版本的朋友)

有以下代码:

import java.io.*;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
import org.apache.poi.util.IOUtils;

public class ExcelDrawImage {

 private static void drawImageOnExcelSheet(XSSFSheet sitePhotosSheet, int row1,
        int row2, int col1, int col2, String fileName) {
    try {
        InputStream is = new FileInputStream(fileName);
        byte[] bytes = IOUtils.toByteArray(is);
        int pictureIdx = sitePhotosSheet.getWorkbook().addPicture(bytes,Workbook.PICTURE_TYPE_JPEG);
        is.close();
        CreationHelper helper = sitePhotosSheet.getWorkbook().getCreationHelper();

        Drawing drawing = sitePhotosSheet.createDrawingPatriarch();

        ClientAnchor anchor = helper.createClientAnchor();
        anchor.setAnchorType(AnchorType.MOVE_AND_RESIZE);

        anchor.setCol1(col1);
        anchor.setCol2(col2);
        anchor.setRow1(row1);
        anchor.setRow2(row2);
        drawing.createPicture(anchor, pictureIdx);
    } catch(Exception e) {
        e.printStackTrace();
    }
 }

 public static void main(String[] args) throws Exception {
  Workbook wb = new XSSFWorkbook();
  Sheet sheet = wb.createSheet();

  for (int r = 0; r < 10000; r+=10 ) {
   sheet.createRow(r).createCell(1).setCellValue("Picture " + (r/10+1) + ":");
   drawImageOnExcelSheet((XSSFSheet)sheet, r+1, r+6, 1, 4, "samplePict.jpeg");
  }

  wb.write(new FileOutputStream("ExcelDrawImage.xlsx"));
  wb.close();
 }
}

结果:

同Ubuntu Linux:

计算结果: