poi shiftRows 调整大小并且不移动图像

poi shiftRows resizes and does not move images

我正在使用 POI,我想在 xlsx 中插入一些行。在这个文件中,我在 table 中有一些图像。当我通过这样做移动行时:

sheet.shiftRows(30, sheet.getLastRowNum(), 10, true, true);

有些图片放错了位置并调整了大小。

如何在不调整图像大小的情况下一次移动所有项目并将它们放在正确的位置?

遇到这个问题,这段代码对我有用 基本上需要做的是更改图像的锚点以反映新的行索引。

XSSFDrawing drawings = mainSheet.createDrawingPatriarch();
for(XSSFShape shape : drawings.getShapes()){
    if(shape instanceof Picture){
        XSSFPicture picture = (XSSFPicture) shape;
        XSSFClientAnchor anchor = picture.getClientAnchor();
        int row1 = anchor.getRow1();
        if (row1 >= 30 && row1 <= mainSheet.getLastRowNum()) {
            int row2 = anchor.getRow2();
            anchor.setRow1(row1 + 10);
            anchor.setRow2(row2 + 10);
        }
     }
 }

事情是这样的:

  • 获取当前sheet中的所有图纸。
  • 检查图像row1锚点是否在移动的行范围内
  • 将新的 row1 锚点设置为旧的 row1 锚点 + 移动的行数
  • 将新的 row2 锚点设置为旧的 row1 锚点 + 移动的行数

抱歉,我无法发表评论。但在我的设置中,John Muiruris 回答的第一行应该是:

XSSFDrawing drawings = (XSSFDrawing) sheet.createDrawingPatriarch();

只是为了避免混淆。