unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, DB, DBTables, Grids, DBGrids, ExtCtrls,olectnrs,comobj,
  OleServer, ExcelXP, Excel2000;type
  TForm1 = class(TForm)
    DataSource1: TDataSource;
    ExcelApplication1: TExcelApplication;
    Panel1: TPanel;
    DBGrid1: TDBGrid;
    Table1: TTable;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
type
  treport1=class(tcomponent)
  private
    row:integer;
    col:integer;
    dataset:tdataset;
    openfilename,savefilename:string;
  protected
    procedure setrowcol(roworcol:integer;value:integer);
  published
    property arow:integer index 0 read row write setrowcol default 0;
    property acol:integer index 1 read col write setrowcol default 0;
    property setdataset:tdataset read dataset write dataset;
  public
    procedure run;
    constructor create(aowner:tcomponent);override;
  end;
var
  Form1: TForm1;
  aa:treport1;
implementation
 var ole,ole1:olevariant;
{$R *.dfm}
constructor treport1.create(aowner:tcomponent);
  begin
    inherited create(aowner);
    row:=1;
    col:=1;
    dataset:=nil;
    openfilename:='';
    savefilename:='';
  end;
procedure treport1.run;
var
  ac,ar,a,b:integer;
begin
 if not dataset.Active then
 begin
   showmessage('数据既没有打开');
   exit;
 end;
 try
   ole:=createoleobject('excel.application');
 except
   showmessage('excel没有安装');
   exit;
 end;
if openfilename='' then
    ole.workbooks.add
else
   ole.workbooks.open(openfilename+'.xls');
   dataset.First;
   dataset.DisableControls;
   ac:=dataset.FieldCount;
   ar:=dataset.RecordCount;
   for a:=1 to dataset.FieldCount do
  // ole.cell[1,a]:=dataset.Fields[a-1].FieldName;     提示错误method cell not 
                                                        supported     by automation object
   for a:=2 to ar+1 do
   begin
     for b:=0 to ac-1 do
        ole.cells[a,b+1]:=dataset.Fields[b].AsString;
        dataset.Next;
   end;
   if savefilename='' then
    savefilename:='songkun';
   if fileexists('d:\songkun.xls') then
     deletefile('d:\songkun.xls');
   ole.activeworkbook.saveas(savefilename+'.xls');
   dataset.EnableControls;
   ole.quit;
   ole:=unassigned;
end;
procedure treport1.setrowcol(roworcol:integer;value:integer);
begin
 if roworcol=0 then
   if value<1 then
     exit
   else
     row:=value;
 if roworcol=1 then
   if value<1 then
     exit
   else
     col:=value;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
aa:=treport1.create(application);
end;procedure TForm1.BitBtn1Click(Sender: TObject);
begin
aa.dataset:=table1;
aa.run;
end;procedure TForm1.BitBtn3Click(Sender: TObject);
begin
 ole1:=createoleobject('excel.application');
 ole1.workbooks.open('d:\songkun.xls');
 ole1.visible:=true;
end;end.

解决方案 »

  1.   

    提示错误method cell not supported     by automation object有高手可以把导出程序发给我吗?? 
    邮箱[email protected]
      

  2.   

    procedure TFrmWIPCheck.PMenuXLSClick(Sender: TObject);  
    var  VarExcel: variant;//定义一个EXCEL变量
    Vari,Varj:integer;      //控制for循环的两个变量即Excel中的行数和列数 
    VarFilePath:String;//存储本程序导出数据文件的路径
    begin
       if Pagecontrol1.ActivePage =TabSheet1 then 
       begin
         if DataModule1.WIPCheckTbl.Active = False then 
         begin
           Showmessage('没有数据可供您输出!');
           Exit;
         end;
         VarExcel := CreateOleObject( 'Excel.Application' );  
         VarExcel.Workbooks.Add;
         VarExcel.Cells[1,1].Value:='盘点单号';
         VarExcel.Cells[1,2].Value:='板号';
         VarExcel.Cells[1,3].Value:='工单号';
         VarExcel.Cells[1,4].Value:='客户名称';
         VarExcel.Cells[1,5].Value:='工单数量';
         VarExcel.Cells[1,6].Value:='PNLS数量';
         VarExcel.Cells[1,7].Value:='UNIT数量';
         VarExcel.Cells[1,8].Value:='报废数量';
         VarExcel.Cells[1,9].Value:='销售订单数量';
         VarExcel.Cells[1,10].Value:='所在部门';
         VarExcel.Cells[1,11].Value:='板的状态';
         VarExcel.Cells[1,12].Value:='盘点日期';
         with DataModule1.WIPCheckTbl do
         begin
           First;
           DisableControls;
           FrmProgress :=TFrmProgress.Create(nil);//创建一个进度条窗体。 
           FrmProgress.Show;
           FrmProgress.ProgressBar1.Max := DataModule1.WIPCheckTbl.RecordCount ;  
           for VarI:=1 to (RecordCount) do 
           begin
             for VarJ:=0 to 11 do 
             begin
               VarExcel.Cells[VarI+1,VarJ+1].Value := Fields[VarJ+1].Text ;  
             end;
             Next;
             FrmProgress.ProgressBar1.StepBy(1);
           end;
         end;
         FrmProgress.Free;
         VarFilePath:='C:\WIP盘点.xls';
         if FileExists(VarFilePath) then
            DeleteFile(VarFilePath);
         VarExcel.ActiveSheet.SaveAs(VarFilePath);
         VarExcel.ActiveWorkBook.Close;
         VarExcel.Quit;
         ShowMessage('C:\WIP盘点.xls,数据导出成功 ,');
         DataModule1.WIPCheckTbl.EnableControls;
      end;
    end;
      

  3.   

    大哥,少写了个s吧,应该是cells[x,y]