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);
                }
            }
        }
    }
}
相关推荐
海南java第二人24 分钟前
Spring MVC核心流程深度解析:从请求到响应的完美掌控
java·springmvc
未来之窗软件服务25 分钟前
幽冥大陆(一百10)PHP打造Java的Jar安全——东方仙盟筑基期
java·php·phar·仙盟创梦ide·东方仙盟
程序猿_极客3 小时前
【2025 年最新版】Java JDK 安装与环境配置教程(附图文超详细,Windows+macOS 通用)
java·开发语言·windows·macos·jdk
猫头虎4 小时前
macOS 双开/多开微信WeChat完整教程(支持 4.X 及以上版本)
java·vscode·macos·微信·编辑器·mac·脚本
二哈喇子!7 小时前
Java开发工具——IDEA(修改全局配置,提升工作效率)
java·编辑器·intellij-idea
强子感冒了7 小时前
Java网络编程学习笔记,从网络编程三要素到TCP/UDP协议
java·网络·学习
二哈喇子!7 小时前
SpringBoot项目右上角选择ProjectNameApplication的配置
java·spring boot
sin22017 小时前
MyBatis的执行流程
java·开发语言·mybatis
二哈喇子!7 小时前
基于Spring Boot框架的车库停车管理系统的设计与实现
java·spring boot·后端·计算机毕业设计
二哈喇子!7 小时前
基于Spring Boot框架的网络游戏虚拟交易平台的设计与实现
java·springboot·毕设项目