不用microsoft.interrop.word 用开源组件如何获取word目录

江湖评谈 2018-03-22 05:10:50
RT:不用microsoft.interrop.word 用开源组件如何获取word目录


有人知道吗?最近这个问题很蛋疼
...全文
454 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
江湖评谈 2018-03-23
  • 打赏
  • 举报
回复
引用 1 楼 Miaonly 的回复:
参考下用Spire.Doc.dll做的Demo,这里获取Word目录考虑了目录内容是否是包含在内容控件里面的两种情况,具体代码如下:
using Spire.Doc;
using Spire.Doc.Fields;
using Spire.Doc.Documents;
using System.Text.RegularExpressions;

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile("example1.docx");
            Section section = document.Sections[0];
            
            Document newDoc = new Document();
            Section newSection = newDoc.AddSection();

            Paragraph p;
            for (int i = 0; i < section.Body.ChildObjects.Count; i++)
            {
                if (section.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.StructureDocumentTag)
                {
                    StructureDocumentTag tag = section.Body.ChildObjects[i] as StructureDocumentTag;
                    foreach (DocumentObject obj in tag.ChildObjects)
                    {
                        if (obj is Paragraph)
                        {
                            if (IsTocParagraph(obj as Paragraph))
                            {
                                newSection.Body.ChildObjects.Add(tag.Clone());
                                break;
                            }
                        }
                    }
                }
                else
                {
                    if (section.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.Paragraph)
                    {
                        p = section.Body.ChildObjects[i] as Paragraph;
                        if (IsTocParagraph(p))
                        {
                            newSection.Body.ChildObjects.Add(p.Clone());
                        }
                        else
                        {
                            if (IsFieldEnd(p))
                            {
                                newSection.Body.ChildObjects.Add(p.Clone());
                            }

                        }
                    }
                }

            }

            string result = "output.docx";
            newDoc.SaveToFile(result, FileFormat.Docx);
            System.Diagnostics.Process.Start(result);

        }

        static bool IsTocParagraph(Paragraph paragraph)
        {
            Regex regex = new Regex("TOC\\w+", RegexOptions.IgnoreCase);
            if (regex.IsMatch(paragraph.StyleName))
            {
                return true;
            }
            return false;
        }

        static bool IsFieldEnd(Paragraph paragraph)
        {
            Body body = paragraph.OwnerTextBody;
            int index = body.ChildObjects.IndexOf(paragraph);
            for (int i = 0; i < paragraph.ChildObjects.Count; i++)
            {
                if (paragraph.ChildObjects[i].DocumentObjectType == DocumentObjectType.FieldMark)
                {
                    if ((paragraph.ChildObjects[i] as FieldMark).Type == FieldMarkType.FieldEnd)
                    {
                        if ((body.ChildObjects[index - 1].DocumentObjectType == DocumentObjectType.Paragraph) && IsTocParagraph(body.ChildObjects[index - 1] as Paragraph))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    }
}
------------------------- 这个不错,有启发,今天上午用NPOI已经搞定了,

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧