我想将一个word文件中内容按word文件表格中的数据逐条取出后存成数据库表的
多条记录,如何将word数据从文件中取出,比如下面的格式 |----|--------|-----------------------------------|
|序号|电压等级|    备注                           |
|----|--------------------------------------------|
|1   |  220kv |                                   |
|-------------------------------------------------|
|..  |..      |..
|    |        |  
当然word的表格向上面的,实线的

解决方案 »

  1.   

    这个问题涉及比较多的内容。
    主要思想,就是通过Delphi建立一个ActiveX COM对象,即Word.Application, 打开一个.Document,然后通过Word的文档对象模型来访问整个问题。在Word软件中能干的活,通过这个文档对象模型都能办到。
    安装Office的时候,要选择“编程参考”,(默认不装的),这样就能在word帮助中看到“与编程有关的信息”,非常有用,包括了word的文档对象模型的完整参考。
    这里给出一个msdn中的例子,其中包括了对表格的访问。
    Building the Automation Sample
    Start Delphi. A new project should be created by default.
    Add a button to Form1.
    Replace the contents of the code window for Unit1 with the following: 
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
        procedure InsertLines(LineNum : Integer);
        procedure CreateMailMergeDataFile;
        procedure FillRow(Doc : Variant; Row : Integer;
                     Text1,Text2,Text3,Text4 : String);
      private
        { Private declarations }  public
        wrdApp, wrdDoc: Variant;
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses ComObj;Const wdAlignParagraphLeft = 0;
    Const wdAlignParagraphCenter = 1;
    Const wdAlignParagraphRight = 2;
    Const wdAlignParagraphJustify = 3;
    Const wdAdjustNone = 0;
    Const wdGray25 = 16;
    Const wdGoToLine = 3;
    Const wdGoToLast = -1;
    Const wdSendToNewDocument = 0;{$R *.DFM}procedure TForm1.InsertLines(LineNum : Integer);
    var
      iCount : Integer;
    begin
      for iCount := 1 to LineNum do
         wrdApp.Selection.TypeParagraph;
    end;procedure TForm1.FillRow(Doc : Variant; Row : Integer;
                     Text1,Text2,Text3,Text4 : String);begin
      Doc.Tables.Item(1).Cell(Row,1).Range.InsertAfter(Text1);
      Doc.Tables.Item(1).Cell(Row,2).Range.InsertAfter(Text2);
      Doc.Tables.Item(1).Cell(Row,3).Range.InsertAfter(Text3);
      Doc.Tables.Item(1).Cell(Row,4).Range.InsertAfter(Text4);
    end;procedure TForm1.CreateMailMergeDataFile;
    var
      wrdDataDoc : Variant;
      iCount : Integer;
    begin
      // Create a data source at C:\DataDoc.doc containing the field data
      wrdDoc.MailMerge.CreateDataSource('C:\DataDoc.doc',,,'FirstName, LastName,' +
           ' Address, CityStateZip');
      // Open the file to insert data
      wrdDataDoc := wrdApp.Documents.Open('C:\DataDoc.doc');
      for iCount := 1 to 2 do
        wrdDataDoc.Tables.Item(1).Rows.Add;
      // Fill in the data
      FillRow(wrdDataDoc, 2, 'Steve', 'DeBroux',
            '4567 Main Street', 'Buffalo, NY  98052');
      FillRow(wrdDataDoc, 3, 'Jan', 'Miksovsky',
            '1234 5th Street', 'Charlotte, NC  98765');
      FillRow(wrdDataDoc, 4, 'Brian', 'Valentine',
            '12348 78th Street  Apt. 214', 'Lubbock, TX  25874');
      // Save and close the file
      wrdDataDoc.Save;
      wrdDataDoc.Close(False);
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      StrToAdd : String;
      wrdSelection, wrdMailMerge, wrdMergeFields : Variant;
    begin
      // Create an instance of Word and make it visible
      wrdApp := CreateOleObject('Word.Application');
      wrdApp.Visible := True;
      // Create a new document
      wrdDoc := wrdApp.Documents.Add();
      wrdDoc.Select;  wrdSelection := wrdApp.Selection;
      wrdMailMerge := wrdDoc.MailMerge;  // Create MailMerge data file
      CreateMailMergeDataFile;
      // Create a string and insert it into the document
      StrToAdd := 'State University' + Chr(13) +
                  'Electrical Engineering Department';
      wrdSelection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
      wrdSelection.TypeText(StrToAdd);  InsertLines(4);
      

  2.   


      // Insert Merge Data
      wrdSelection.ParagraphFormat.Alignment := wdAlignParagraphLeft;
      wrdMergeFields := wrdMailMerge.Fields;  wrdMergeFields.Add(wrdSelection.Range,'FirstName');
      wrdSelection.TypeText(' ');
      wrdMergeFields.Add(wrdSelection.Range,'LastName');
      wrdSelection.TypeParagraph;
      wrdMergeFields.Add(wrdSelection.Range,'Address');
      wrdSelection.TypeParagraph;
      wrdMergeFields.Add(wrdSelection.Range,'CityStateZip');  InsertLines(2);  // Right justify the line and insert a date field with
      // the current date
      wrdSelection.ParagraphFormat.Alignment := wdAlignParagraphRight;
      wrdSelection.InsertDateTime('dddd, MMMM dd, yyyy',False);  InsertLines(2);  // Justify the rest of the document
      wrdSelection.ParagraphFormat.Alignment := wdAlignParagraphJustify;  wrdSelection.TypeText('Dear ');
      wrdMergeFields.Add(wrdSelection.Range,'FirstName');  wrdSelection.TypeText(',');
      InsertLines(2);  // Create a string and insert it into the document
      StrToAdd := 'Thank you for your recent request for next ' +
          'semester''s class schedule for the Electrical ' +
          'Engineering Department.  Enclosed with this ' +
          'letter is a booklet containing all the classes ' +
          'offered next semester at State University.  ' +
          'Several new classes will be offered in the ' +
          'Electrical Engineering Department next semester.  ' +
          'These classes are listed below.';
      wrdSelection.TypeText(StrToAdd);  InsertLines(2);  // Insert a new table with 9 rows and 4 columns
      wrdDoc.Tables.Add(wrdSelection.Range,9,4);
      wrdDoc.Tables.Item(1).Columns.Item(1).SetWidth(51,wdAdjustNone);
      wrdDoc.Tables.Item(1).Columns.Item(2).SetWidth(170,wdAdjustNone);
      wrdDoc.Tables.Item(1).Columns.Item(3).SetWidth(100,wdAdjustNone);
      wrdDoc.Tables.Item(1).Columns.Item(4).SetWidth(111,wdAdjustNone);
      // Set the shading on the first row to light gray  wrdDoc.Tables.Item(1).Rows.Item(1).Cells
          .Shading.BackgroundPatternColorIndex := wdGray25;
      // BOLD the first row
      wrdDoc.Tables.Item(1).Rows.Item(1).Range.Bold := True;
      // Center the text in Cell (1,1)
      wrdDoc.Tables.Item(1).Cell(1,1).Range.Paragraphs.Alignment :=
            wdAlignParagraphCenter;  // Fill each row of the table with data
      FillRow(wrdDoc, 1, 'Class Number', 'Class Name', 'Class Time', 
         'Instructor');
      FillRow(wrdDoc, 2, 'EE220', 'Introduction to Electronics II',
         '1:00-2:00 M,W,F', 'Dr. Jensen');
      FillRow(wrdDoc, 3, 'EE230', 'Electromagnetic Field Theory I',
         '10:00-11:30 T,T', 'Dr. Crump');
      FillRow(wrdDoc, 4, 'EE300', 'Feedback Control Systems',
         '9:00-10:00 M,W,F', 'Dr. Murdy');
      FillRow(wrdDoc, 5, 'EE325', 'Advanced Digital Design',
         '9:00-10:30 T,T', 'Dr. Alley');
      FillRow(wrdDoc, 6, 'EE350', 'Advanced Communication Systems',
         '9:00-10:30 T,T', 'Dr. Taylor');
      FillRow(wrdDoc, 7, 'EE400', 'Advanced Microwave Theory',
         '1:00-2:30 T,T', 'Dr. Lee');
      FillRow(wrdDoc, 8, 'EE450', 'Plasma Theory',
         '1:00-2:00 M,W,F', 'Dr. Davis');
      FillRow(wrdDoc, 9, 'EE500', 'Principles of VLSI Design',
         '3:00-4:00 M,W,F', 'Dr. Ellison');  // Go to the end of the document  wrdApp.Selection.GoTo(wdGotoLine,wdGoToLast);
      InsertLines(2);  // Create a string and insert it into the document
      StrToAdd := 'For additional information regarding the ' +
                 'Department of Electrical Engineering, ' +
                 'you can visit our website at ';
      wrdSelection.TypeText(StrToAdd);
      // Insert a hyperlink to the web page
      wrdSelection.Hyperlinks.Add(wrdSelection.Range,'http://www.ee.stateu.tld');
      // Create a string and insert it into the document
      StrToAdd := '.  Thank you for your interest in the classes ' +
                 'offered in the Department of Electrical ' +
                 'Engineering.  If you have any other questions, ' +
                 'please feel free to give us a call at ' +
                 '555-1212.' + Chr(13) + Chr(13) +
                 'Sincerely,' + Chr(13) + Chr(13) +
                 'Kathryn M. Hinsch' + Chr(13) +
                 'Department of Electrical Engineering' + Chr(13);
      wrdSelection.TypeText(StrToAdd);  // Perform mail merge
      wrdMailMerge.Destination := wdSendToNewDocument;
      wrdMailMerge.Execute(False);  // Close the original form document
      wrdDoc.Saved := True;
      wrdDoc.Close(False);  // Notify the user we are done.
      ShowMessage('Mail Merge Complete.');  // Clean up temp file
      DeleteFile('C:\DataDoc.doc');end;end.
      

  3.   

    OLE了
    用D6/7的TWordApplication也是可以的
      

  4.   

    在 Delphi 程序中直接调用 Word 文档并使用 Word 的功能
       方法一: 
      1) 从 WORD.EXE 中导入 WORD 的 Type Library 
      2) var wd : _Application; 
       wdt : Table; 
       dot1,FileName: OleVariant; 
      begin 
       dot1 := 'c:\abc.dot'; 
       wd := coApplication_.Create; 
       wd.Documents.Add(dot1, EmptyParam); 
       wdt := wd.ActiveDocument.Tables.Item(1); 
       wdt.Cell(1,3).Range.Text := 'Hello!'; 
       //以下是保存和关闭文档及 Word, 可以不关,下面两个例程略过此步。 
       //EmptyParam 用来代替不使用的参数。 
       FileName := 'c:\test.doc'; 
       wd.ActiveDocument.SaveAs(FileName,EmptyParam,EmptyParam, 
       EmptyParam, 
         EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam, 
         EmptyParam, 
         EmptyParam); 
       wd.ActiveDocument.Close(EmptyParam,EmptyParam,EmptyParam); 
       wd.Quit(EmptyParam,EmptyParam,EmptyParam); 
      end;   方法二: 
      var 
       Ole: Variant; 
       NewDoc: Variant; 
       DocTable: Variant; 
      begin 
      //方法二的参数可以直接使用 Delphi 的数据类型。其它方法只能使用 OleVariant 类型. 
        Ole := CreateOleObject('Word.Application'); 
        Ole.Application.Visible := true; 
        NewDoc := Ole.Documents.Add('C:\abc.dot'); 
        DocTable := NewDoc.Tables.Item(1); 
        DocTable.Cell(1,3).Range.Text := 'Hello!'; 
      //设置Variant对象为UnAssigned来释放该对象的引用。 
        NewDoc := UnAssigned; 
        DocTable := UnAssigned; 
        Ole := UnAssigned; 
      end;   方法三:使用 Server 页的 TWordApplication 控件,同理,你可以用里头的所有控件。Ole 为TWordApplication控件名称。
      var 
       NewDoc: Variant; 
       DocTable: Variant; 
      o: OleVariant; 
      begin 
       o := 'c:\abc.dot'; 
       NewDoc := Ole.Documents.Add(o,EmptyParam); 
       DocTable := NewDoc.Tables.Item(1); 
       DocTable.Cell(1,3).Range.Text := 'Hello!'; 
       NewDoc := UnAssigned; 
       DocTable := UnAssigned; 
      end;    推荐用方法一和方法三。因为方法二不提供类型参数检查,也因为方法二的接口调用效率低。程序中所使用的属性和方法资料可以在 Office 的 VBA 帮助中查到。需要者请在安装 Office 时选择安装 VBA 编程帮助。
      

  5.   

    这个例子或者有用的
    http://www.2ccc.com/article.asp?articleid=16
      

  6.   

    var
      AppWord,MyDoc,R,T:variant;
      filename:string;
      i:integer;
    begin
        filename:='c:\lxj.doc';
        try
          AppWord := CreateOleObject('Word.Application');
        except
          MessageDlg('请确定Microsoft Word是否安装!', mtInformation, [mbOK], 0);
          appword.quit;
          abort;
        end;
        if not FileExists(filename) then begin
          showmessage('C:\bdzyxyb.doc不存在!');
          appword.quit;
          abort;
        end;
        //增加模板
        MyDoc := AppWord.Documents.Add(Template:='c:\lxj.doc');
        T := MyDoc.tables.item(1);
        R := T.Cell(i,1).Range;   { for i:=1 to 60 do
        begin
          T := MyDoc.tables.item(1);
          R := T.Cell(i,1).Range;
          showmessage(r.text);
        end;}如上:每次都自动启动word,而且提示只能作为一个副本打开,
    for 循环上面可以每次取出一个表格的内容,我想用循环处理
    却报异常。而且怎么知道表格中有多少行记录?