1:如何将多个DBGrid中的内容导到一个Word文档的多个表格中?
2:如何能得到一个word文档中有几个表格(表格不嵌套)?
补充:(1)word文档中事先没有表格,表格由程序自动生产(这个我会)。
(2)现在的问题是多个表格位置的控制,如何才不至于表格都嵌套在一起。

解决方案 »

  1.   

    (1)word文档中事先没有表格,表格由程序自动生产(这个我会)。不好意思,这个怎么做啊?谢谢
      

  2.   

    可以先看看我的拙作,也许对你有用。
    http://blog.csdn.net/neowang/archive/2004/10/08/127522.aspx
      

  3.   

    to:taiguang(银狐) 网上搜的unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      ExtCtrls, DBCtrls, Grids, DBGrids, StdCtrls, Db, DBTables, OleServer,
      Word97, clipbrd, Word2000, WordXP, ADODB;
    type
      TMyFirstThread = class(TThread)
      private  protected
        procedure Execute;override;
      end;
    type
      TForm1 = class(TForm)
        ADOConnection1: TADOConnection;
        ADOQuery1: TADOQuery;
        DBGrid1: TDBGrid;
        DataSource1: TDataSource;
        Button1: TButton;
        Button2: TButton;
        WordDocument: TWordDocument;
        WordApplication: TWordApplication;
        WordFont: TWordFont;
        ChkBoxNewDoc: TCheckBox;
        ADOQuery2: TADOQuery;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        procedure SetFontSize();
      public
        Thread1:TMyFirstThread;
      end;var
      Form1: TForm1;implementationuses Variants;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
      ADOQuery1.Close;
      ADOQuery1.SQL.Text:='SELECT * FROM PT_WSMS WHERE (WSLX=''0'' OR WSLX=''1'' OR WSLX=''5'' OR WSLX=''6'') AND (Ltrim(Rtrim(HelpTitle))='''' OR HelpTitle IS NULL)';
      ADOQuery1.Open;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      Thread1:=TMyFirstThread.Create(False);
      Thread1.Priority:=tpNormal;
      Thread1.FreeOnTerminate:=True;
    end;procedure TForm1.SetFontSize;
    begin
      WordFont.ConnectTo(WordDocument.Sentences.Get_Last.Font);
      WordFont.Name:='宋体';
      WordFont.Size:=9;
    end;{ TMyFirstThread }procedure TMyFirstThread.Execute;
    var
      Docs, Template, NewTemplate, ItemIndex: OleVariant;
      i,j:integer;
      RecCount:integer;
    begin
      with Form1 do
      begin
      try
        Template := EmptyParam;
        NewTemplate := True;
        ItemIndex := 1;
        try
          Wordapplication.Connect;
        except
          MessageDlg('Word没有安装!', mtError, [mbOk], 0);
          Abort;
        end;
        Wordapplication.Visible := True;
        WordApplication.Caption := '文书详细';
        Template := EmptyParam;                  {创建新的文档}
        NewTemplate := False;
        Docs := WordApplication.Documents;
        Docs.Add(NewTemplate := True);
        WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));     {WordDocument组件赋值}
        WordApplication.Options.CheckSpellingAsYouType := False;                  {关闭拼写检查,如果进行拼写检查,将减缓Winword速度}
        WordApplication.Options.CheckGrammarAsYouType := False;
        WordDocument.Sentences.Last.Paste;
        WordDocument.Range.Text := '';                                           {插入数据}
        i:=1;
        while not  ADOQuery1.Eof do
        begin
          j:=6;
          ADOQuery2.Close;
          ADOQuery2.SQL.Text:='select sysobjects.name as tablename,syscolumns.name as columnname,sysproperties.value as discription from ';
          ADOQuery2.SQL.Add('sysobjects,syscolumns,sysproperties where  ');
          ADOQuery2.SQL.Add('sysobjects.name='''+Trim(ADOQuery1.FieldByName('TableName').AsString)+''' and syscolumns.id = sysobjects.id  and  sysproperties.smallid= syscolumns.colid and sysproperties.id = sysobjects.id');
          ADOQuery2.Open;
          RecCount:=ADOQuery2.RecordCount+5;
          WordDocument.Tables.Add(WordDocument.Words.Last,RecCount,4,EmptyParam,EmptyParam);
          WordDocument.Range.InsertAfter(''  + #13);
          WordDocument.Tables.Item(i).Cell(1,1).Merge(WordDocument.Tables.Item(i).Cell(1,2));
          WordDocument.Tables.Item(i).Cell(1,2).Merge(WordDocument.Tables.Item(i).Cell(1,3));
          WordDocument.Tables.Item(i).Cell(2,1).Merge(WordDocument.Tables.Item(i).Cell(2,2));
          WordDocument.Tables.Item(i).Cell(2,2).Merge(WordDocument.Tables.Item(i).Cell(2,3));
          WordDocument.Tables.Item(i).Cell(3,1).Merge(WordDocument.Tables.Item(i).Cell(3,2));
          WordDocument.Tables.Item(i).Cell(3,2).Merge(WordDocument.Tables.Item(i).Cell(3,3));
          WordDocument.Tables.Item(i).Cell(4,1).Merge(WordDocument.Tables.Item(i).Cell(4,4));
          WordDocument.Tables.Item(i).Cell(1,1).Range.Text := '文书名称';
          SetFontSize;
          WordDocument.Tables.Item(i).Cell(1,2).Range.Text := ADOQuery1.FieldByName('WritName').AsString;
          SetFontSize;
          WordDocument.Tables.Item(i).Cell(2,1).Range.Text := '对应表名';
          SetFontSize;
          WordDocument.Tables.Item(i).Cell(2,2).Range.Text := ADOQuery2.FieldByName('TableName').AsString;
          SetFontSize;
          WordDocument.Tables.Item(i).Cell(3,1).Range.Text := 'ID号';
          SetFontSize;
          WordDocument.Tables.Item(i).Cell(3,2).Range.Text := ADOQuery1.FieldByName('ID').AsString;
          SetFontSize;
          WordDocument.Tables.Item(i).Cell(5,1).Range.Text := '字段名';
          SetFontSize;
          WordDocument.Tables.Item(i).Cell(5,2).Range.Text := '中文名称';
          SetFontSize;
          WordDocument.Tables.Item(i).Cell(5,3).Range.Text := '是否系统生成';
          SetFontSize;
          WordDocument.Tables.Item(i).Cell(5,4).Range.Text := '是否必填字段';
          SetFontSize;
          while not ADOQuery2.Eof do
          begin
            WordDocument.Tables.Item(i).Cell(j,1).Range.Text:=ADOQuery2.FieldByName('columnname').AsString;
            SetFontSize;
            WordDocument.Tables.Item(i).Cell(j,2).Range.Text:=ADOQuery2.FieldByName('discription').AsString;
            SetFontSize;
            j:=j+1;
            ADOQuery2.Next;
          end;
          ADOQuery1.Next;
          i:=i+1;
        end;
      except
        on E: Exception do
        begin
          ShowMessage(E.Message);
          WordApplication.Disconnect;
        end;
      end;
      end;
    end;end.
      

  4.   

    这是我做的导出Word的代码。看看对你有用。基本上都满足了你的问题。关于方法,属性不懂的可以查看帮助。
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      ExtCtrls, DBCtrls, Grids, DBGrids, StdCtrls, Db, DBTables, OleServer,
      Word97, clipbrd, Word2000, WordXP, ADODB;
    type
      TMyFirstThread = class(TThread)
      private  protected
        procedure Execute;override;
      end;
    type
      TForm1 = class(TForm)
        ADOConnection1: TADOConnection;
        ADOQuery1: TADOQuery;
        DBGrid1: TDBGrid;
        DataSource1: TDataSource;
        Button1: TButton;
        Button2: TButton;
        WordDocument: TWordDocument;
        WordApplication: TWordApplication;
        WordFont: TWordFont;
        ChkBoxNewDoc: TCheckBox;
        ADOQuery2: TADOQuery;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        procedure SetFontSize();
      public
        Thread1:TMyFirstThread;
      end;var
      Form1: TForm1;implementationuses Variants;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
      ADOQuery1.Close;
      ADOQuery1.SQL.Text:='SELECT * FROM PT_WSMS WHERE (WSLX=''0'' OR WSLX=''1'' OR WSLX=''5'' OR WSLX=''6'') AND (Ltrim(Rtrim(HelpTitle))='''' OR HelpTitle IS NULL)';
      ADOQuery1.Open;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      Thread1:=TMyFirstThread.Create(False);
      Thread1.Priority:=tpNormal;
      Thread1.FreeOnTerminate:=True;
    end;procedure TForm1.SetFontSize;
    begin
      WordFont.ConnectTo(WordDocument.Sentences.Get_Last.Font);
      WordFont.Name:='宋体';
      WordFont.Size:=9;
    end;{ TMyFirstThread }procedure TMyFirstThread.Execute;
    var
      Docs, Template, NewTemplate, ItemIndex: OleVariant;
      i,j:integer;
      RecCount:integer;
    begin
      with Form1 do
      begin
      try
        Template := EmptyParam;
        NewTemplate := True;
        ItemIndex := 1;
        try
          Wordapplication.Connect;
        except
          MessageDlg('Word没有安装!', mtError, [mbOk], 0);
          Abort;
        end;
        Wordapplication.Visible := True;
        WordApplication.Caption := '文书详细';
        Template := EmptyParam;                  {创建新的文档}
        NewTemplate := False;
        Docs := WordApplication.Documents;
        Docs.Add(NewTemplate := True);
        WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));     {WordDocument组件赋值}
        WordApplication.Options.CheckSpellingAsYouType := False;                  {关闭拼写检查,如果进行拼写检查,将减缓Winword速度}
        WordApplication.Options.CheckGrammarAsYouType := False;
        WordDocument.Sentences.Last.Paste;
        WordDocument.Range.Text := '';                                           {插入数据}
        i:=1;
        while not  ADOQuery1.Eof do
        begin
          j:=6;
          ADOQuery2.Close;
          ADOQuery2.SQL.Text:='select sysobjects.name as tablename,syscolumns.name as columnname,sysproperties.value as discription from ';
          ADOQuery2.SQL.Add('sysobjects,syscolumns,sysproperties where  ');
          ADOQuery2.SQL.Add('sysobjects.name='''+Trim(ADOQuery1.FieldByName('TableName').AsString)+''' and syscolumns.id = sysobjects.id  and  sysproperties.smallid= syscolumns.colid and sysproperties.id = sysobjects.id');
          ADOQuery2.Open;
          RecCount:=ADOQuery2.RecordCount+5;
          WordDocument.Tables.Add(WordDocument.Words.Last,RecCount,4,EmptyParam,EmptyParam);
          WordDocument.Range.InsertAfter(''  + #13);
          WordDocument.Tables.Item(i).Cell(1,1).Merge(WordDocument.Tables.Item(i).Cell(1,2));
          WordDocument.Tables.Item(i).Cell(1,2).Merge(WordDocument.Tables.Item(i).Cell(1,3));
          WordDocument.Tables.Item(i).Cell(2,1).Merge(WordDocument.Tables.Item(i).Cell(2,2));
          WordDocument.Tables.Item(i).Cell(2,2).Merge(WordDocument.Tables.Item(i).Cell(2,3));
          WordDocument.Tables.Item(i).Cell(3,1).Merge(WordDocument.Tables.Item(i).Cell(3,2));
          WordDocument.Tables.Item(i).Cell(3,2).Merge(WordDocument.Tables.Item(i).Cell(3,3));
          WordDocument.Tables.Item(i).Cell(4,1).Merge(WordDocument.Tables.Item(i).Cell(4,4));
          WordDocument.Tables.Item(i).Cell(1,1).Range.Text := '文书名称';
          SetFontSize;
          WordDocument.Tables.Item(i).Cell(1,2).Range.Text := ADOQuery1.FieldByName('WritName').AsString;
          SetFontSize;
          WordDocument.Tables.Item(i).Cell(2,1).Range.Text := '对应表名';
          SetFontSize;
          WordDocument.Tables.Item(i).Cell(2,2).Range.Text := ADOQuery2.FieldByName('TableName').AsString;
          SetFontSize;
          WordDocument.Tables.Item(i).Cell(3,1).Range.Text := 'ID号';
          SetFontSize;
          WordDocument.Tables.Item(i).Cell(3,2).Range.Text := ADOQuery1.FieldByName('ID').AsString;
          SetFontSize;
          WordDocument.Tables.Item(i).Cell(5,1).Range.Text := '字段名';
          SetFontSize;
          WordDocument.Tables.Item(i).Cell(5,2).Range.Text := '中文名称';
          SetFontSize;
          WordDocument.Tables.Item(i).Cell(5,3).Range.Text := '是否系统生成';
          SetFontSize;
          WordDocument.Tables.Item(i).Cell(5,4).Range.Text := '是否必填字段';
          SetFontSize;
          while not ADOQuery2.Eof do
          begin
            WordDocument.Tables.Item(i).Cell(j,1).Range.Text:=ADOQuery2.FieldByName('columnname').AsString;
            SetFontSize;
            WordDocument.Tables.Item(i).Cell(j,2).Range.Text:=ADOQuery2.FieldByName('discription').AsString;
            SetFontSize;
            j:=j+1;
            ADOQuery2.Next;
          end;
          ADOQuery1.Next;
          i:=i+1;
        end;
      except
        on E: Exception do
        begin
          ShowMessage(E.Message);
          WordApplication.Disconnect;
        end;
      end;
      end;
    end;end.
      

  5.   

    这个是在word中创建一个表的程序:
     ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
            5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
            wdAutoFitFixed
        With Selection.Tables(1)
            If .Style <> "网格型" Then
                .Style = "网格型"
            End If
            .ApplyStyleHeadingRows = True
            .ApplyStyleLastRow = True
            .ApplyStyleFirstColumn = True
            .ApplyStyleLastColumn = True
        End With
        ShowVisualBasicEditor = True
    关于定位问题 有两种方法:
    1。用书签
    2。在理面定义特殊字符,查找这个字符的位置
    你应该用第一种方法!
      

  6.   

    To ghy412(用心良苦):
      Word文档是程序自动创建的,里面没有书签。
    现在的关键问题是我创建完了一个表格之后,如何“跳”出来,再去创建下一个表格。关键是如何跳。