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

我使用poi 3.9来创buildxlsx文件,一切正常,直到我的文件长度超过1600行 。 跨过1600行后,我能写数据,但我无法写图像所有图像相互追加在行号1640这是奇怪的,我正在poi从长时间,并select了它的库限制和更新的问题我的诗到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(); } 

注意:我能够写入数据,但只能为图像问题。 请build议我如何解决这个问题。 请看下面的图像,在这里你可以看到,在第1639行,两个图像相互追加,这两个图像后面有很多图像,因为我在控制台上打印的最后一行图像是3400。 在这里输入图像说明

使用apache poi版本3.16和Java 8(我不是古代软件版本的朋友)

有以下代码:

 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中也一样:

在这里输入图像说明

计算结果:

在这里输入图像说明