C # generated word document directory

Add a reference to Microsoft.Office.Interop.Word

 

 New Class Library project, the preparation method of build directory

  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 is          /// document production directory
 32          ///  </ Summary> 
33 is          ///  <param name = "oToc"> build directory location tag </ param> 
34 is          ///  <param name = "oFindText1"> a directory tag </ param> 
35          ///  <param name = "oFindText2"> secondary directory tag </ param> 
36          ///  <param name = "oFindText3"> three directory tag </ param> 
37 [          ///  <Returns> whether to generate a successful </ 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, refoMissing, REF oMissing))
 84                      {
 85                          p.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
 86                          p.Range.Font.Bold = . 1 ;
 87                          p.Range.Font.ColorIndex = WdColorIndex.wdBlue;
 88                          // put directory region 
89                          p.Range.InsertParagraphAfter ();
 90                          myRange = p.Next () the Range;.
 91 is                          // put page break area 
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 In Flag;
 124          }
 125  
126  
127          ///  <Summary> 
128          /// close the document release resources
 129          ///  </ Summary> 
130.          public  void the Close ()
 131 is          {
 132              doc.close ( REF oMissing, REF oMissing, REF oMissing);
 133              oWord.Quit ( REF oMissing, REF oMissing, REF oMissing);
 134          }
 135     }
136 
137 }
View Code

 

test

. 1          Private  void btnMyDOc_Click ( Object SENDER, EventArgs E)
 2          {
 . 3              MyDocManager myDocManager = new new MyDocManager ();
 . 4              Object oFindText1 = " [Title. 1] " ;
 . 5              Object oFindText2 = " [Title 2] " ;
 . 6              Object oFindText3 = " [ title. 3] " ;
 . 7              Object oToc = " [Contents] " ;
 . 8              myDocManager.OpenDocument ( @"C:\Users\Administrator\Desktop\a.docx");
 9             myDocManager.GenerateToc( oToc, oFindText1, oFindText2, oFindText3);
10             myDocManager.Close();
11         }
View Code

 

Guess you like

Origin www.cnblogs.com/kispine/p/11965140.html