在網上搜了半天沒有直截了當的方法,要麼就是很慢,要麼就是要命的慢,通過自己的琢磨,終於能夠快速獲取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;
}