C# 生成word文档目录

添加对Microsoft.Office.Interop.Word引用

C# 生成word文档目录

 

 新建类库项目,编写生成目录方法

C# 生成word文档目录
  1 using Microsoft.Office.Interop.Word;
  2 using System;
  3 
  4 namespace MyDoc
  5 {
  6     public class MyDocManager
  7     {
  8         private object format = WdSaveFormat.wdFormatDocument;//保存格式
  9         private Object oMissing = System.Reflection.Missing.Value;
 10         private Object oTrue = true;
 11         private Object oFalse = false;
 12         private Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
 13         private Microsoft.Office.Interop.Word.Document doc = null;
 14         //分页符
 15         private object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
 16 
 17 
 18         /// <summary>
 19         /// 打开文档
 20         /// </summary>
 21         /// <param name="path"></param>
 22         public void OpenDocument(object path)
 23         {
 24             doc = oWord.Documents.Open(ref path,
 25             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
 26             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
 27             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); 
 28         }
 29 
 30         /// <summary>
 31         /// 文档生产目录
 32         /// </summary>
 33         /// <param name="oToc">生成目录位置标签</param>
 34         /// <param name="oFindText1">一级目录标签</param>
 35         /// <param name="oFindText2">二级目录标签</param>
 36         /// <param name="oFindText3">三级目录标签</param>
 37         /// <returns>是否生成成功</returns>
 38         public bool GenerateToc(object oToc, object oFindText1, object oFindText2, object oFindText3)
 39         {
 40             bool flag = true;
 41             if (doc == null)
 42             {
 43                 return false;
 44             } 
 45             object space = "";
 46             object toc = "目录";
 47             object oTocFormat = Microsoft.Office.Interop.Word.WdTocFormat.wdTOCClassic;
 48 
 49             Range myRange = null ;
 50 
 51             foreach (Paragraph p in doc.Paragraphs)
 52             {
 53                 if (p.Range.Text.Contains(oFindText1.ToString()))
 54                 {
 55                     if (p.Range.Find.Execute(ref oFindText1, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
 56                         ref oMissing, ref oMissing, ref space, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing))
 57                     {
 58                         p.OutlineLevel = WdOutlineLevel.wdOutlineLevel1;
 59                     }
 60                 }
 61                 if (p.Range.Text.Contains(oFindText2.ToString()))
 62                 {
 63                     if (p.Range.Find.Execute(ref oFindText2, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
 64                         ref oMissing, ref oMissing, ref space, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing))
 65                     {
 66                         p.OutlineLevel = WdOutlineLevel.wdOutlineLevel2;
 67                     }
 68                 }
 69 
 70                 if (p.Range.Text.Contains(oFindText3.ToString()))
 71                 {
 72 
 73                     if (p.Range.Find.Execute(ref oFindText3, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
 74                         ref oMissing, ref oMissing, ref space, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing))
 75                     {
 76                         p.OutlineLevel = WdOutlineLevel.wdOutlineLevel3;
 77                     }
 78                 }
 79 
 80                 if (p.Range.Text.Contains(oToc.ToString()))
 81                 {
 82                     if (p.Range.Find.Execute(ref oToc, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
 83                         ref oMissing, ref oMissing, ref toc, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing))
 84                     {
 85                         p.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
 86                         p.Range.Font.Bold = 1;
 87                         p.Range.Font.ColorIndex = WdColorIndex.wdBlue;
 88                         //放目录区域
 89                         p.Range.InsertParagraphAfter();
 90                         myRange = p.Next().Range;
 91                         //放分页符区域
 92                         p.Range.InsertParagraphAfter();
 93                         p.Next().Next().Range.InsertBreak(ref oPageBreak);
 94                     }
 95                 }
 96             }
 97            
 98             object x = 0;
 99             for (int i = 1; i <= doc.TablesOfContents.Count; i++)
100             {
101                 doc.TablesOfContents[i].Range.Delete();
102             }
103             
104             Object oUpperHeadingLevel = "1";
105             Object oLowerHeadingLevel = "3";
106             Object oTOCTableID = "TableOfContents";
107             if (myRange != null)
108             {
109 
110                 doc.TablesOfContents.Add(myRange, ref oTrue, ref oUpperHeadingLevel,
111                     ref oLowerHeadingLevel, ref oMissing, ref oTOCTableID, ref oTrue,
112                     ref oTrue, ref oMissing, ref oTrue, ref oTrue, ref oTrue);
113                 oWord.ActiveDocument.TablesOfContents[1].TabLeader = WdTabLeader.wdTabLeaderDots;
114                 oWord.ActiveDocument.TablesOfContents.Format = Microsoft.Office.Interop.Word.WdTocFormat.wdTOCClassic;
115                 oWord.ActiveDocument.TablesOfContents[1].Update();
116             }
117             else
118             {
119                 flag = false;
120             }
121 
122             doc.Save();
123             return flag;
124         }
125 
126 
127         /// <summary>
128         /// 关闭文档释放资源
129         /// </summary>
130         public void Close()
131         {
132             doc.Close(ref oMissing, ref oMissing, ref oMissing);
133             oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
134         }
135     }
136 
137 }
View Code

 

测试

C# 生成word文档目录
 1         private void btnMyDOc_Click(object sender, EventArgs e)
 2         {
 3             MyDocManager myDocManager = new MyDocManager();
 4             object oFindText1 = "[标题1]";
 5             object oFindText2 = "[标题2]";
 6             object oFindText3 = "[标题3]";
 7             object oToc = "[目录]";
 8             myDocManager.OpenDocument(@"C:\Users\Administrator\Desktop\a.docx");
 9             myDocManager.GenerateToc( oToc, oFindText1, oFindText2, oFindText3);
10             myDocManager.Close();
11         }
View Code

 

C# 生成word文档目录

上一篇:DLL Injection with Delphi(转载)


下一篇:C# WinForm界面美化--使用IrisSkin实现换肤功能