怎么在Word里画表格?

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/3370/3370314.xml?temp=.9221002
    代码里有简单的画表格
      

  2.   

    那你就在菜单里找‘表格-插入-表格’就行了,你到底在想什么?难道用DELPHI在WORD里画表吗?
      

  3.   

    chinaliujacky(失落之城)  
     可爱了。
      

  4.   

    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.