集合所有用Delphi做Office开发者的精妙技巧,强力对抗阵营!!

解决方案 »

  1.   

    是让大家贴出Delphi做Office开发的技巧,对抗VB阵营
      

  2.   

    先来一段抛砖引玉,简单操纵WORD
    procedure TFm_Main.BitBtn1Click(Sender: TObject);
    var ConfirmConversions, ReadOnly, AddToRecentFiles,
        PassWordDocument, PassWordTemplate, Revert,
        WritePassWordDocument, WritePassWordTemplate, Format,
        ExcelFileName, ItemIndex:OleVariant;
        i,j:Integer;
        Start_Cell,End_Cell,CellRange:OleVariant;
    begin
      With Q1,Parameters do begin
        Close;
        SQL.Clear;
        SQL.Add('select * from ryxxb ');
        Open;
      end;
      ExcelFileName:=GetCurrentDir()+'\Table.doc';
      if not FileExists(ExcelFileName) then begin
        MessageBox(self.Handle,'报表模板文件不存在,无法打开!','错误',MB_ICONERROR);
        Exit;
      end;
      ConfirmConversions := False;
      ReadOnly := False;
      AddToRecentFiles := False;
      PassWordDocument := '';
      PassWordTemplate := '';
      Revert := True;
      WritePassWordDocument := '';
      WritePassWordTemplate := '';
      Format := WDOpenFormatDocument;
      WordApp.Documents.OpenOld(ExcelFileName, ConfirmConversions,
         ReadOnly, AddToRecentFiles, PassWordDocument, PassWordTemplate,
         Revert, WritePassWordDocument, WritePassWordTemplate, Format );
      ItemIndex:=1;
      WordDoc.ConnectTo(WordApp.Documents.Item(ItemIndex));
      WordApp.Options.CheckSpellingAsYouType:=False;
      WordApp.Options.CheckGrammarAsYouType:=False;
      ReplaceText('<#Name>','数码科技有限公司');
      WordApp.WindowState:=wdWindowStateMaximize;
      WordApp.Visible:=True;
      WordDoc.ActiveWindow.ActivePane.View.SeekView:=wdSeekCurrentPageHeader;
      WordApp.Selection.TypeText('综合管理方案书');
      WordApp.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
      WordDoc.ActiveWindow.ActivePane.View.SeekView:=wdSeekCurrentPageFooter;
      WordApp.Selection.TypeText('制表日期:'+DateTimeToStr(Now));
      WordApp.Selection.ParagraphFormat.Alignment:=wdAlignParagraphRight;
      WordDoc.ActiveWindow.ActivePane.View.SeekView:=wdSeekMainDocument;
      //WordDoc.PageSetup.Orientation:=wdOrientPortrait;
      WordDoc.PageSetup.PaperSize:=wdPaperA4;
      WordDoc.PageSetup.Orientation:=wdOrientLandScape;
      WordDoc.Range.Tables.AddOld(WordDoc.Range,Q1.RecordCount+1,Q1.FieldCount);
      Start_Cell:=WordDoc.Range.Tables.Item(1).Cell(1,1);
      End_Cell:=WordDoc.Range.Tables.Item(1).Cell(1,5);
      Start_Cell.Merge(End_Cell);
      CellRange:=Start_Cell.Range;
      CellRange.InsertAfter('姓名');
      CellRange.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
      //CellRange.Style:=wdStyleHeading3;
      for i:=1 to Q1.RecordCount do begin
        for j:=1 to 5 do begin
          WordDoc.Range.Tables.Item(1).Cell(i+1,j).Range.InsertAfter(Q1.Fields[j].AsString);
        end;
        Q1.Next;
      end;
    end;
    procedure TFm_Main.ReplaceText(sFindText,sReplaceText:String);
    var
      FindText, MatchCase, MatchWholeWord, MatchWildCards, MatchSoundsLike,
      MatchAllWordForms, Fforward, FWrap, FFormat, ReplaceWith, Replace,
      MatchKashida,MatchDiacritics,MachAlefHamza,MatchControl: OleVariant;
    begin
      FindText := sFindText;
      MatchCase := False;
      MatchWholeWord := True;
      MatchWildCards := False;
      MatchSoundsLike := False;
      MatchAllWordForms := False;
      FForward := True;
      FWrap := WDFindContinue;
      FFormat := False;
      ReplaceWith := sReplaceText;
      Replace := True;
      WordDoc.Range.Find.Execute(FindText, MatchCase, MatchWholeWord,
          MatchWildCards, MatchSoundsLike, MatchAllWordForms,FForward,
          FWrap, FFormat, ReplaceWith, Replace,MatchKashida,MatchDiacritics,MachAlefHamza,MatchControl);
    end;
      

  3.   

    我操纵excel,这个是在我的数据库程序里面的一个应用阿!!procedure TForm7.Button5Click(Sender: TObject);
    var
      Range:excelrange;
      i,j,k:integer;
      letter:char;
    begin
    if datasource1.DataSet=nil then showmessage('没有内容可以导入Excel!')
    else begin
      try
        try
        excelapplication1.Connect;
        except
        showmessage('您的机器没有安装Excel!');
        abort;
        end;
        excelapplication1.Visible[0]:=true;
        excelapplication1.Caption:='送修内容';
        excelapplication1.Workbooks.Add(NUll,0);    k:=datasource1.DataSet.FieldCount-4;    if datasource1.DataSet.Fields[k].DisplayLabel='COUNT OF 局名' then
         k:=4
        else k:=0;    i:=65+datasource1.DataSet.FieldCount-1-k;
        letter:=char(i);    range:=excelapplication1.Range['A1',letter+'1'];    for i:=0 to datasource1.DataSet.FieldCount-1-k do
          begin
          range.Value:=datasource1.DataSet.Fields[i].DisplayLabel;
          range:=range.Next;
          end;
        if k=4 then begin
        range.Value:='数量';
        dec(k);
        end;
        datasource1.DataSet.First;    for i:=1 to datasource1.DataSet.RecordCount do begin
          range:=excelapplication1.Range['A'+inttostr(i+1),letter+inttostr(i+1)];
          for j:=0 to datasource1.DataSet.FieldCount-1-k do begin
            range.Value:=datasource1.DataSet.Fields[j].AsString ;
            range:=range.Next;
            end;
          datasource1.DataSet.Next;
          end;  except
      excelapplication1.Disconnect;
      end;  end;
    end;
      

  4.   

    操作execlprocedure SaveStringListToExcel(sTitle: string; aList: TStringList; sMask: Char);
    var
      TempSaveDialog: TSaveDialog;
      TempExcel: Variant;
      TempWorkBook: OleVariant;
      TempWorkSheet: OleVariant;
      //TempRange: OleVariant;
      //TempChart: OleVariant;
      TempFileName: string;
      I,J: Integer;
      iColCount, iRowCount: integer;
    begin
      TempSaveDialog:=TSaveDialog.Create(nil);
      TempSaveDialog.Filter:='Microsoft Excel 文件(*.xls)';
      if not TempSaveDialog.Execute then
      begin
        TempSaveDialog.Free;
        exit;
      end;
      if TempSaveDialog.FileName='' then
      begin
        TempSaveDialog.Free;
        exit;
      end;  TempFileName:=TempSaveDialog.FileName;
      //创建Exell对象
      try
        TempExcel:=CreateOleObject('Excel.Application');
        TempExcel.Application.Visible:=False;
        TempExcel.Application.WorkBooks.Add;
        TempWorkBook:=TempExcel.Application.WorkBooks[1];
        TempWorkSheet:=TempWorkBook.WorkSheets[1];
      except
        MessageDlg('初始化Excel失败,可能没装Excel!',mtConfirmation,[mbOK],0);
        TempSaveDialog.Free;
        TempExcel.Quit;
        TempExcel:=Unassigned;
      end;
      //导出数据到Exell文件
      iRowCount := aList.Count;
      TempWorkSheet.Cells[1, 1] := sTitle;  for I := 0 to  iRowCount -1 do
      begin
        iColCount := GetColCount(aList[I], sMask);
        for J := 0 to iColCount -  1 do
          TempWorkSheet.Cells[I + 2, J + 1] := '''' + GetCol(aList[I], J, '|');
      end;
      TempWorkBook.SaveAs(TempFileName);
      TempSaveDialog.Free;
      TempExcel.Quit;
      TempExcel:=Unassigned;end;
      

  5.   


    我也来一段搜索一个字符串在WORD文件中是否存在?先拖一个命令按钮、一个LABEL、一个EDIT、一个OpenDialog 控件放到窗体上。unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, OleServer, Word2000;type
      TForm1 = class(TForm)
        Edit1: TEdit;
        Button1: TButton;
        Label1: TLabel;
        Label2: TLabel;
        OpenDialog1: TOpenDialog;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses  ComObj;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
      msWord:variant;begin
      if OpenDialog1.Execute then
      begin
        msWord:=CreateOleObject('Word.Application');    msWord.Documents.Open(OpenDialog1.FileName);    msWord.Visible:=False; //为True则WORD应用程序可见!    msWord.ActiveDocument.Content.Find.MatchWholeWord:=True;
        msWord.ActiveDocument.Content.Find.MatchCase:=False;
        msWord.ActiveDocument.Content.Find.ClearFormatting;    if msWord.ActiveDocument.Content.Find.execute(Edit1.Text)=True then
           Label2.Caption:='找到'
        else
           Label2.Caption:='没找到';
        msWord.ActiveDocument.close(SaveChanges:=False);
        msWord.Quit;  end;end;end.