Java 复制Word表格中的行或列

2025-11-09 04:13:18

1、方法1:通过eiceblue官网下载jar包。下载后,解压文件,并将lib文件夹下的Spire.Doc.jar文件导入到java程序。参考如下导入效果购游:

Java 复制Word表格中的行或列

2、方何边法2:可通过maven仓库安装墨暗帮导入。

1、import com.spire.doc.*;

public class CopyRow {

    public static void main(String[] args) {  

        //加载测试文档

        Document doc = new Document();

        doc.loadFromFile("test.docx");

        //获取表格

        Section section = doc.getSections().get(0);

        Table table =section.getTables().get(0);

        //复制第三行,并将复制后的行插入到表格作为第五行

        TableRow row = table.getRows().get(2).deepClone();

        table.getRows().insert(4,row);

        //保存文档

        doc.saveToFile("CopyRow.docx",FileFormat.Docx_2013);

        doc.dispose();

    }

}

2、表格行复制效果:

Java 复制Word表格中的行或列

1、import com.spire.doc.*;

public class CopyColumn {

    public static void main(String[] args) {

//加载测试文档

        Document doc = new Document();

        doc.loadFromFile("test.docx");

        //获取表格

        Section section = doc.getSections().get(0);

        Table table =section.getTables().get(0);

        //遍历表格每行

        for (int i = 0; i < table.getRows().getCount(); i++) {

            //复制表格中每行的最后一个单元格,复制,并新添加一列到表格

            TableRow row = table.getRows().get(i);

            TableCell cell = (TableCell) row.getCells().getLastItem().deepClone();

            row.getCells().add(cell);

        }

        //保存文档

        doc.saveToFile("CopyColumn.docx",FileFormat.Docx_2013);

        doc.dispose();

    }

}

2、表格列复制效果:

Java 复制Word表格中的行或列

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢