添加更多图像时POI的问题

我想编辑一个Excel文件里面的图像,并添加更多的图像。 这是我的testing代码:

1 File inputFile=new File(path); 2 InputStream inp = new FileInputStream(inputFile); 3 Workbook wb = WorkbookFactory.create(inp); 4 InputStream is = new FileInputStream("catalog.png"); 5 byte[] bytes = IOUtils.toByteArray(is); 6 int picture=wb.addPicture(bytes, wb.PICTURE_TYPE_PNG); 7 Drawing drawing = sheet.createDrawingPatriarch(); 8 //drawing = ((XSSFSheet)sheet).getDrawingPatriarch(); //missing method?? 9 CreationHelper helper = wb.getCreationHelper(); 10 ClientAnchor anchor = helper.createClientAnchor(); 11 anchor.setCol1(0); 12 anchor.setRow1(currentRow.getRowNum()); 13 anchor.setCol2(1); 14 anchor.setRow2(currentRow.getRowNum()+1); 15 Picture pict = drawing.createPicture(anchor, picture); 

在第6行的addPicture方法使其他现有的图像被破坏(一个红色的X出现,而不是图像),那么我想我需要一个getDrawingPathiarch()方法,但只有createDrawingPatriarch(),这将破坏现有的图像…我处理这个?

使用HSSF,您只能添加新的图像,不能编辑现有图像,或追加更多图像。 他们都需要一口气进去

.xlsx文件格式更为清晰,因此XSSF可以让您将新的图像添加到已有的文件中