当前位置:软件学习 > Word >>

C# 快速获取Word文档结构

在网上搜了半天没有直截了当的方法,要么就是很慢,要么就是要命的慢,通过自己的琢磨,终于能够快速获取word文档的结构了。晒一晒,(*^__^*) 嘻嘻……。

[csharp] 
private void barButtonItem24_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) 
       { 
           Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)(this.axFramerControl1.ActiveDocument); 
 
           object x = doc.GetCrossReferenceItems(Microsoft.Office.Interop.Word.WdReferenceType.wdRefTypeHeading); 
           Array strs = (Array)x; 
           int pid = 0; 
           int id = 0; 
           System.Data.DataTable dt = new PrintHelper().DTStruct(); 
           //doc.Application.ScreenUpdating = false; 
           int UpLevel = 0; 
           DataRow drUP = null; 
           for (int i = 0; i < strs.Length; i++) 
           { 
               object count = i + 1; 
               string str = strs.GetValue(i + 1).ToString(); 
               string strtrm = str.TrimStart(); 
               DataRow dr = dt.NewRow(); 
               dr["SortFlag"] = i + 1; 
               dr["ParagraphIndex"] = i + 1; 
               dr["Name"] = strtrm;                 
               int level = str.Length - strtrm.Length; 
               dr["Level"] = level;                
               if (level == 0) 
               { 
                   dr["ParentID"] = 0; 
                   UpLevel = 0; 
                   object Heading = (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToHeading; 
                   doc.Application.Selection.GoTo(ref Heading, ref missing, ref count, ref missing); 
                   dr["SectionNumber"] = doc.Application.Selection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdActiveEndSectionNumber);                
               
               } 
               else 
               { 
                   if (level - UpLevel == 0) 
                   { 
                       dr["ParentID"] = drUP["ParentID"]; 
                       dr["SectionNumber"] = drUP["SectionNumber"]; 
                       
                   } 
                   else 
                       if (level - UpLevel > 0) 
                       { 
                           dr["ParentID"] = drUP["ID"]; 
                           dr["SectionNumber"] = drUP["SectionNumber"]; 
                       } 
                       else 
                       { 
                           dr["ParentID"] = ParetnID(i - 1, dt, level)["ParentID"]; 
                           dr["SectionNumber"] = ParetnID(i - 1, dt, level)["SectionNumber"]; 
                       } 
                
                
               } 
               dt.Rows.Add(dr); 
 &

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,