Delphi and Word (2) get data in the Word table

xiaoxiao2021-03-06  61

// data acquisition procedure getWordCellStr Word table; var WordApp: TWordApplication; WordDoc: TWordDocument; DocInx, oFileName, CfCversions, oReadOnly, AddToRctFiles, PswDocument, PswTemplate, oRevert, WPswDocument, WPswTemplate, oFormat: OleVariant; i, iRow, iCol: INTEGER; Mycell: Cell; MyRow: Row; Begin Memo1.Lines.clear;

/ / ===== Create an object ===== if Not Assigned (WordApp) Then Begin Wordapp: = TWORDAPPLICATION.CREATE (NIL); Wordapp.Visible: = false; end; if not assigned (WordDoc) THEN WordDoc: = TWordDocument.Create (nil); try DocInx: = 1; oFileName: = 'd: /test.doc'; oReadOnly: = true; CfCversions: = EmptyParam; AddToRctFiles: = EmptyParam; PswDocument: = EmptyParam; PswTemplate: = EmptyParam ; oRevert: = EmptyParam; WPswDocument: = EmptyParam; WPswTemplate: = EmptyParam; oFormat: = EmptyParam; // ===== open the file ===== WordApp.Documents.open (oFileName, CfCversions, oReadOnly, AddToRctFiles, PswDocument , Pswtemplate, Orevert, WPswdocument, WPSWTemplate, OFORMAT, / / ​​===== Associated file ===== WordDoc.connectto (Wordapp.Documents.Item (Docinx));

// Method (1) ==> Rule Table for i: = 1 to WordDoc.tables.count DO / / IROW line for irow: = 1 to WordDoc.tables.Item (i). Rows.Count Do Begin // Part ICOL Column for ICol: = 1 to WordDoc.Tables.Item (i) .Columns.count Do Begin Mycell: = WordDoc.tables.Item (i) .Cell (IROW, ICOL); MEMO1 .Lines.Add (mycell.range.text); End; end; end; // method (2) ==> irregular table: only horizontal merged time for i: = 1 to worddoc.tables.count do // i table begin for irow: = 1 to worddoc.tables.Item (i) .Rows.count do beg myrow: = worddoc.tables.Item (i) .Rows.Item (irow); // 第 行行 for iCol : = 1 to myrow.cells.count do // Part ICOL column Begin mycell: = myrow.cells.item (iCol); memo1.Lines.Add (mycell.range.text); End; end; end; // (3) ==> Irregular: Time to landscape, longitudinally; any table for i: = 1 to worddoc.tables.count do // 第 第 第 Begin //Th J: = 1 to WordDoc.tables.Item (i) .Range.cells.count do begin mycell: = worddoc.tables.Item (i) .range.cells.Item (j); MEMO1 .Lines.Add (mycell.range.text); end;

转载请注明原文地址:https://www.9cbs.com/read-119068.html

New Post(0)