哈哈,给分吧,我帮你搞定!

解决方案 »

  1.   

    unit Main;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, OleServer, Word2000, ExtCtrls, DB, DBTables;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        WordApplication: TWordApplication;
        OpenDialog: TOpenDialog;
        WordDocument: TWordDocument;
        FromEdit: TLabeledEdit;
        ToEdit: TLabeledEdit;
        Query5: TQuery;
        DataSource5: TDataSource;
        Query6: TQuery;
        DataSource6: TDataSource;
        ComboBox1: TComboBox;
        Memo1: TMemo;
        WordFont: TWordFont;
        WordParagraphFormat1: TWordParagraphFormat;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
        TableNo: Integer;
        MSTables: Tables;
        OldProp,DefaultTableBehavior,AutoFitBehavior: OleVariant;
        procedure MoveDown(Selecting: Boolean = FALSE);
        procedure MoveRight(Selecting: Boolean = FALSE);
        procedure SetHilight;
        procedure SetNormal;
        procedure SetFont(
          Bold:Boolean = FALSE;
          Italic:Boolean = FALSE;
          UnderLine:Boolean = FALSE;
          Emboss:Boolean = FALSE;
          Engrave:Boolean = FALSE;
          Shadow:Boolean = FALSE;
          DoubleStrike:Boolean = FALSE;
          Strike:Boolean = FALSE;
          FontSize: Integer = 9);
        procedure GenerateWordDocumentExt;
      public
        { Public declarations }
      end;
    var
      Form1: TForm1;implementation{$R *.dfm}
    function ReplaceText(Source,Replaced:String; ReplaceWith: String = ''): String;
    var Index: Integer;
        Len: Integer;
    begin
      Result := Source;
      Index := Pos(Replaced, Result);
      Len := Length(Replaced);
      while Index > 0 do
      begin
        Delete(Result,Index,Len);
        Insert(ReplaceWith,Result,Index);
        Index := Pos(Replaced, Result);
      end;
    end;procedure TForm1.SetFont(
          Bold:Boolean = FALSE;
          Italic:Boolean = FALSE;
          UnderLine:Boolean = FALSE;
          Emboss:Boolean = FALSE;
          Engrave:Boolean = FALSE;
          Shadow:Boolean = FALSE;
          DoubleStrike:Boolean = FALSE;
          Strike:Boolean = FALSE;
          FontSize: Integer = 9);
    begin
    //  WordFont.ConnectTo(WordDocument.Sentences.Get_Last.Font);
    //  WordFont.ConnectTo(WordDocument.Range.Font);
      WordFont.ConnectTo( WordApplication.Selection.Font );
      if Underline    then WordFont.Underline := 2           else WordFont.Underline := 0;
      if Bold         then WordFont.Bold      := 1           else WordFont.Bold      := 0;
      if Italic       then WordFont.Italic    := 1           else WordFont.Italic    := 0;
      if Emboss       then WordFont.Emboss    := 1           else WordFont.Emboss    := 0;
      if Engrave      then WordFont.Engrave   := 1           else WordFont.Engrave   := 0;
      if Shadow       then WordFont.Shadow    := 1           else WordFont.Shadow    := 0;
      if DoubleStrike then WordFont.DoubleStrikeThrough := 1 else WordFont.DoubleStrikeThrough := 0;
      if Strike       then WordFont.StrikeThrough := 1       else WordFont.StrikeThrough := 0;  WordFont.Size := FontSize;
      WordFont.Name := '宋体';
    end;{启动WORD}
    procedure TForm1.Button1Click(Sender: TObject);
    var ItemIndex,Template,NewTemplate: OleVariant;begin
      WordApplication.Options.CheckSpellingAsYouType := False;
      WordApplication.Options.CheckGrammarAsYouType := False;  try
        Wordapplication.Connect;
      except
        MessageDlg('Word may not be installed', mtError, [mbOk], 0);
        Abort;
      end;
      Wordapplication.Visible := True;
      WordApplication.Caption := 'Delphi automation';  {New Document}
      Template := EmptyParam;
      NewTemplate := False;  WordApplication.Documents.Add(Template, NewTemplate,emptyparam,emptyparam);  {Assign WordDocument component}
      ItemIndex := 1;
      WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));  Button2.Enabled := TRUE;
    //  ComboBox1Change(ComboBox1);
      TableNo := 1;end;{插入表格及内容}
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      WordApplication.Options.CheckSpellingAsYouType := False;
      WordApplication.Options.CheckGrammarAsYouType := False;  MSTables := WordDocument.Tables;  SetFont;
      OldProp := WordApplication.Selection.Get_Style;  DefaultTableBehavior := wdWord9TableBehavior; //EmptyParam;
      AutoFitBehavior := wdAutoFitContent;// EmptyParam;//wdAutoFitWindow;//wdAutoFitFixed;  GenerateWordDocumentExt;end;
    procedure TForm1.MoveDown(Selecting: Boolean = FALSE);
    var vUnit,{vCount,}vExtend: OleVariant;
    begin
      vUnit := wdStory;//wdLine;
      //vCount := 1000;//ACount;
      if Selecting then vExtend := wdExtend
      else vExtend := EmptyParam;
      //WordApplication.Selection.MoveDown(vUnit,vCount,vExtend);
      WordApplication.Selection.EndKey(vUnit,VExtend);
    end;procedure TForm1.MoveRight(Selecting: Boolean = FALSE);
    var vUnit,{vCount,}vExtend: OleVariant;
    begin
      vUnit := wdLine;//wdCharacter;
      //vCount := 1000;//ACount;
      if Selecting then vExtend := wdExtend
      else vExtend := EmptyParam;
    //  WordApplication.Selection.MoveRight(vUnit,vCount,vExtend);
      WordApplication.Selection.EndKey(vUnit,vExtend);
    end;procedure TForm1.SetHilight;
    begin
      WordApplication.Selection.Font.Bold := 1;
      WordApplication.Selection.Font.Color := wdColorTeal;
    end;procedure TForm1.SetNormal;
    begin
      WordApplication.Selection.Font.Bold := 0;
      WordApplication.Selection.Font.Color := wdColorAutomatic;
    end;
    procedure TForm1.GenerateWordDocumentExt;
    var Prop,ItemIndex,vAddress,vSubAddress,vScreenTip,vTextToDisp,vTarget:OleVariant;
        I,RecordCount,J: Integer;
        MSTable: Table;
        MSCol: Column;
        TempStr: String;
    begin
      {插入一级目录}
      WordDocument.Range.InsertAfter('枚举类型定义'#13);
      ItemIndex := 'Heading 1';
      Prop := WordDocument.Styles.Item(ItemIndex);
      WordApplication.Selection.Set_Style(Prop);
      MoveDown;  WordApplication.Selection.Set_Style(OldProp);
      WordDocument.Range.InsertAfter(#13);
      MoveDown;  if not Query6.Active then Query6.Open;
      if not Query5.Active then Query5.Open;  Query6.First;
      while not Query6.Eof do
      begin
        RecordCount := 0;
        Query5.First;
        while not Query5.Eof do
        begin
          Inc(RecordCount);
          Query5.Next;
        end;    if RecordCount > 0 then
        begin
          {插入二级目录}
          ItemIndex := 'Heading 2';
          Prop := WordDocument.Styles.Item(ItemIndex);
          WordApplication.Selection.Set_Style(Prop);
          TempStr := Format('%s[%s]',
            [Query6.FieldByName('cname').AsString,
             Query6.FieldByName('ord').AsString]);
          WordDocument.Range.InsertAfter(TempStr + #13);
          MoveDown;
          WordApplication.Selection.Set_Style(OldProp);
          MSTables.Add(WordApplication.Selection.Range,RecordCount+1,2,DefaultTableBehavior,AutoFitBehavior);
          MSTable := MSTables.Item(TableNo);
          Inc(TableNo);
          MSTable.Range.Font.Bold := 0;
          MSTable.Range.Font.Color := wdColorAutomatic;
          MSTable.Range.Font.Size := 9;
          MsTable.Rows.Item(1).Range.Font.Bold := 1;
          MSTable.Cell(1,1).Range.Text := '枚举值';
          MSTable.Cell(1,2).Range.Text := '含义';
          MoveDown;      Query5.First;
          I := 2;
          while not Query5.Eof do
          begin
            MSTable.Cell(I,1).Range.Text := Query5.FieldByName('val').AsString;
            MSTable.Cell(I,2).Range.Text := Query5.FieldByName('strs').AsString;
            Inc(I);
            Query5.Next;
          end;
          WordDocument.Range.InsertAfter(#13);
          MoveDown;
        end;
        Query6.Next;
      end;end;