java操作word里的表格

复制代码
依赖:
<dependency>
    <groupId>com.techCoLtd</groupId>
    <artifactId>aspose-words-16.4.0-jdk16</artifactId>
    <classifier>jdk16</classifier>
</dependency>

/**
 * 删除表格及表格的行
 * @throws Exception
 */
private static void deletedTable() throws Exception {
    Document doc = new Document("表格路径");
    Table table = doc.getFirstSection().getBody().getTables().get(0);
    // 删除整个表的方法
    table.remove();
    // 删除某一行 的方法
    if (table.getRows().getCount() > 1) { // 确保至少有两行才进行删除操作
        table.getRows().removeAt(0);// 删除第一行
    }

    doc.save("D:\\work\\save\\ceshi.docx");
}
复制代码
/**
 *
 * 读取表格的内容
 * @throws Exception
 */
public static void readTableTxtByIndex() throws Exception {
    Document doc = new Document("表格路径");
    //获取所有的表格
    NodeCollection tableNodes = doc.getChildNodes(NodeType.TABLE, true);
    int tableCount = tableNodes.getCount();
    System.out.println("表格数量:{}"+ tableCount);
    for (int i = 0; i < tableCount; i++) {
        Table table = (Table)tableNodes.get(i);
        //按表格索引 单独写逻辑进行数据的读取 本示例中就1个表格 因此就放1个逻辑进行解析了
        if(0 == i){
            //获取表格中的所有行节点
            RowCollection rows = table.getRows();
            int rowCount = rows.getCount();
            System.out.println("共有表格行:{}" + rowCount);
            //获取每一行的单元格节点
            for (int j = 0; j < rowCount; j++) {
                Row row = rows.get(j);
                CellCollection cells = row.getCells();
                int cellsCount = cells.getCount();
                System.out.println("第" + j + "行有" + rowCount + "个单元格{}");
                for (int k = 0; k < cellsCount; k++) {
                    Cell cell = cells.get(k);
                    String cellText = cell.getText();
                    System.out.println("第" + j + "行 第" + k + "个单元格的文本:{}" + cellText);
                }
            }
        }
    }
}
相关推荐
叫我阿柒啊4 分钟前
从Java全栈到前端框架:一位程序员的实战之路
java·spring boot·微服务·消息队列·vue3·前端开发·后端开发
mqiqe23 分钟前
架构-亿级流量性能调优实践
java·架构
野犬寒鸦1 小时前
力扣hot100:旋转图像(48)(详细图解以及核心思路剖析)
java·数据结构·后端·算法·leetcode
七夜zippoe1 小时前
AI+Java 守护你的钱袋子!金融领域的智能风控与极速交易
java·人工智能·金融
岁忧1 小时前
(LeetCode 面试经典 150 题) 200. 岛屿数量(深度优先搜索dfs || 广度优先搜索bfs)
java·c++·leetcode·面试·go·深度优先
liliangcsdn2 小时前
结合prompt分析NodeRAG的build过程
java·服务器·人工智能·数据分析·知识图谱
黑色的山岗在沉睡2 小时前
LeetCode 189. 轮转数组
java·算法·leetcode
会飞的小蛮猪3 小时前
Jenkins运维之路(权限分配&忘记admin密码)
java·运维·经验分享·jenkins·prometheus
slim~3 小时前
Java基础第9天总结(可变参数、Collections、斗地主)
java·开发语言