程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 快速獲取Word文檔結構

C# 快速獲取Word文檔結構

編輯:C#入門知識

在網上搜了半天沒有直截了當的方法,要麼就是很慢,要麼就是要命的慢,通過自己的琢磨,終於能夠快速獲取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); 
              UpLevel=level; 
               drUP = dr; 
           } 
          // doc.Application.ScreenUpdating = true;             
 
       } 
       DataRow ParetnID(int rowindex, System.Data.DataTable dt, int level) 
       { 
         //  object parentID = DBNull.Value; 
           DataRow dr=dt.NewRow(); 
           for (int i = rowindex; i >= 0; i--) 
           { 
               if (dt.Rows[i]["Level"].ToString() == level.ToString()) 
               { 
                   dr = dt.Rows[i]; 
                   break; 
               }   www.2cto.com
           } 
           return dr; 
       } 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved