After searching the Internet for half a day, there is no straightforward method, either it is slow or it is terrible slow. by thinking about it, you can finally get the structure of the Word document quickly. Take a shower, (* ^__^ *), and you will see it .......
[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;
}