孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,ExcelXP, StdCtrls,ComObj,OleServer,OleCtrls, ComCtrls;

procedure TForm1.btn1Click(Sender: TObject);               //创建EXCEL并赋值
begin
    if VarIsEmpty(ExlApp) then
    ExlApp:= CreateOleObject('Excel.Application');
    if VarIsEmpty(WorkBook) then
    ExlApp.displayAlerts:=False;
    ExlApp.ScreenUpdating:=False;
    ExlApp.WorkBooks.Open(ExtractFilePath(Application.ExeName)+'a.xlsx');
    WorkBook:= ExlApp.WorkBooks[1];
    ExlApp.WorkSheets[1].Activate;

    ExlApp.Cells[1,1].Value:='fdauto';
    ExlApp.Cells[1,2].Value:='fdauto1';
    ExlApp.Cells[1,3].Value:='fdauto2';
    ExlApp.Cells[1,4].Value:='fdauto3';
    ExlApp.Cells[1,5].Value:='fdauto4';
    ExlApp.Cells[1,6].Value:='fdauto5';
    ExlApp.Cells[1,7].Value:='fdauto6';
    ExlApp.Cells[2,1].Value:='aaa';
    ExlApp.Cells[2,2].Value:='aaa1';
    ExlApp.Cells[2,3].Value:='aaa2';
    ExlApp.Cells[2,4].Value:='aaa3';
    ExlApp.Cells[2,5].Value:='aaa4';
    ExlApp.Cells[2,6].Value:='aaa5';
    ExlApp.Cells[2,7].Value:='aaa6';
    ExlApp.Visible:=False;
end;


procedure TForm1.btn3Click(Sender: TObject);         \\关闭EXCEL
begin
  if not VarIsEmpty(ExlApp) then begin //释放对象
    ExlApp.activeWorkBook.Saved:=True;
    ExlApp.displayAlerts:=True;
    ExlApp.ScreenUpdating:=True;
    ExlApp.WorkBooks.Close;
    ExlApp.Quit;
    end
  else
    if VarIsEmpty(ExlApp) then
    begin
       Application.MessageBox('对象不存在', '提示',
       MB_OK + MB_ICONINFORMATION);
       Exit;
    end;
end;


procedure TForm1.btn4Click(Sender: TObject); //写EXCEL
var
  iRow,iCol,MaxRow,MaxColumn:Integer;
  FName:TStringList;
begin
    if VarIsEmpty(ExlApp) then
    begin
     Application.MessageBox('对象不存在', '提示',
    MB_OK + MB_ICONINFORMATION);
    Exit;
    end;
    iRow:=StrToInt(edt2.Text);
    iCol:=StrToInt(edt3.Text);
    FName:=TStringList.Create;
    ExlApp.WorkSheets[1].Cells[iRow,iCol]:=edt1.Text;
end;


EXCEL和word都是OLE对象   ,你把EXCEL改成WORD  基本一个做法。

上面的方法属于动态创建。(就是用代码)
当然你也可以用delphi控件搞定
posted on 2011-01-10 16:51  孤独的猫  阅读(302)  评论(0)    收藏  举报