C# 生成word文檔目錄


添加對Microsoft.Office.Interop.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

 

測試

 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM