Apache poi克隆工作表部分复制注释(注释存在但没有绘图)。
创始人
2024-09-04 17:00:51
0次

要克隆Apache POI工作表的部分并复制注释(注释存在但没有绘图),可以按照以下步骤进行操作:

  1. 首先,使用POI库加载现有的工作簿文件。
Workbook workbook = WorkbookFactory.create(new FileInputStream("input.xlsx"));
  1. 然后,获取要克隆的源工作表和目标工作表的索引。
int sourceSheetIndex = 0; // 要克隆的源工作表索引
int targetSheetIndex = 1; // 目标工作表的索引
  1. 接下来,使用源工作表索引获取源工作表对象。
Sheet sourceSheet = workbook.getSheetAt(sourceSheetIndex);
  1. 然后,使用源工作表对象创建目标工作表对象。
Sheet targetSheet = workbook.cloneSheet(targetSheetIndex);
  1. 现在,我们需要复制源工作表的注释到目标工作表。首先,获取源工作表的所有注释。
Drawing drawing = sourceSheet.createDrawingPatriarch();
List comments = drawing.getComments();
  1. 然后,使用目标工作表的绘图对象,循环遍历源工作表的注释,并将它们复制到目标工作表。
Drawing targetDrawing = targetSheet.createDrawingPatriarch();
for (Comment comment : comments) {
    ClientAnchor anchor = comment.getClientAnchor();
    Comment newComment = targetDrawing.createCellComment(anchor);
    newComment.setString(comment.getString());
    targetDrawing.createCellComment(newComment);
}
  1. 最后,保存工作簿到文件或流中。
workbook.write(new FileOutputStream("output.xlsx"));
workbook.close();

完整的代码示例如下:

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

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class CloneWorksheetWithCommentsExample {
    public static void main(String[] args) throws IOException {
        Workbook workbook = WorkbookFactory.create(new FileInputStream("input.xlsx"));

        int sourceSheetIndex = 0; // 要克隆的源工作表索引
        int targetSheetIndex = 1; // 目标工作表的索引

        Sheet sourceSheet = workbook.getSheetAt(sourceSheetIndex);
        Sheet targetSheet = workbook.cloneSheet(targetSheetIndex);

        Drawing drawing = sourceSheet.createDrawingPatriarch();
        List comments = drawing.getComments();

        Drawing targetDrawing = targetSheet.createDrawingPatriarch();
        for (Comment comment : comments) {
            ClientAnchor anchor = comment.getClientAnchor();
            Comment newComment = targetDrawing.createCellComment(anchor);
            newComment.setString(comment.getString());
            targetDrawing.createCellComment(newComment);
        }

        workbook.write(new FileOutputStream("output.xlsx"));
        workbook.close();
    }
}

请注意,此示例假设输入文件为XLSX格式,并且源工作表和目标工作表的索引已根据实际情况进行设置。

相关内容

热门资讯

分享给玩家!wepoker智能... 分享给玩家!wepoker智能辅助插件,wepoker透视脚本(先前是有辅助)AI 是一款可以让一直...
最新技巧!wepokerplu... 最新技巧!wepokerplus万能挂,德扑之星能不能开挂神器(竟然有辅助器)主打本地运行的 AI ...
玩家攻略!wepoker脚本下... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成wepoker脚本下载解析、 AI ...
2025版教程!wpk插件,德... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
免费测试版!wepoker私人... 您好:wepoker私人局有透视吗这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现...
玩家爆料!wepoker好友局... 玩家爆料!wepoker好友局透视,德普之星开挂透视教程插件(固有存在有透视挂)AI 是一款可以让一...
避坑细节!wpk俱乐部辅助器,... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
AI教程!wepoker公共底... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成wepoker公共底牌解析、 AI ...
一秒答解!wpk辅助哪里买,w... 一秒答解!wpk辅助哪里买,wepoker可以开挂吗神器(固有真的有辅助器)AI 智能体黑科技再上新...
一分钟了解!!wpk辅助工具下... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...