关于java使用spire.doc-free实现word内的需求

本文介绍了Spire.doc,一个强大的Word类库,通过示例展示了如何获取文档节、使用正则表达式搜索文本、创建文档对象、获取书签关联的表格以及操作书签。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        spire.doc是一个很强大的word类库,隶属与冰蓝科技,它简化了对word开发的代码量,下面有我对spire.doc总结的一写工具类:

       1.获取文档内所有节
    /**
     * 获取所有节
     *
     * @param document 文档对象
     * @return
     */
    public static List<String> getSection(Document document) {
        ArrayList<String> list = new ArrayList<>();
        SectionCollection sections = document.getSections();
        for (int i = 1; i <= sections.getCount(); i++) {
            list.add("节" + i);
        }
        return list;
    }
2. 使用正则表达式找到您想匹配的文本内容
public static List<String> getPlaceholder(Document document, String regular) {
        ArrayList<String> list = new ArrayList<>();
        Pattern compile = Pattern.compile(regular);

        TextSelection[] textSelections = document.findAllPattern(compile, true);
        if (textSelections != null) {
            for (TextSelection selection : textSelections) {
                TextRange[] results = selection.getAsRange();
                for (TextRange result : results) {
                    list.add(result.getText());
                }
            }
        }
        return list;
    }
3.获取文档对象
 /**
     * 获取文档对象
     *
     * @param filePath 文档路径
     * @return
     */
    public static Document getDocument(String filePath) {
        Document document = new Document();
        document.loadFromFile(filePath);
        return document;
    }
4.获取书签对应的表格
/**
     * 获取书签对应的所有表格
     *
     * @param
     * @return
     */
    public static ArrayList<Table> BookGetTable(Document document) {
        ArrayList<Table> tables = new ArrayList<>();
        ArrayList<Bookmark> bookMark = getBookMarks(document);
        for (int i = 0; i < bookMark.size(); i++) {
            if (bookMark.get(i).getName().contains("Table")
                    && !bookMark.get(i).getName().contains("DivName")
                    && !bookMark.get(i).getName().contains("Remark")
            ) {
                for (int z = 0; z < bookMark.size(); z++) {
                    Paragraph paragraph = bookMark.get(z).getBookmarkStart().getOwnerParagraph();
                    if (paragraph.isInCell()) {
                        //通过书签获取表格
                        Table table = (Table) paragraph.getOwner().getOwner().getOwner();
//                        if (table != null) {
                        tables.add(table);
//                        }
                    }
                }
            }
        }
        return tables;
    }
5.获取指定书签对应表格
/**
     * 获取指定书签对应表格
     *
     * @param bookmark
     * @return
     */
    public static Table getTable(Bookmark bookmark) {
        if (bookmark.getName().contains("Table")
                && !bookmark.getName().contains("DivName")
                && !bookmark.getName().contains("Remark")
        ) {
            Paragraph paragraph = bookmark.getBookmarkStart().getOwnerParagraph();
            if (paragraph.isInCell()) {
                //通过书签获取表格
                Table table = (Table) paragraph.getOwner().getOwner().getOwner();
                return table;
            }
        }
        return null;
    }
6.获取文档内所有书签
/**
     * 获取某个文档中所有书签
     *
     * @param
     * @return ArrayList<String>书签名称集合
     */
    public static ArrayList<Bookmark> getBookMarks(Document doc) {
        ArrayList<Bookmark> arrayList = new ArrayList<>();
//        获取所有书签
        BookmarkCollection bookmarks = doc.getBookmarks();
        for (int i = 0; i <= bookmarks.getCount() - 1; i++) {
            //获取每个书签
            Bookmark bookmark = bookmarks.get(i);
            //获取每个书签名称
//            String name = bookmark.getName();
            //将书签名称保存到list集合里
            if ("_GoBack".equals(bookmark.getName())) {
                continue;
            }
            arrayList.add(bookmark);
        }
        return arrayList;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值