怎样用DELPHI把SQL SERVER2000数据导出到EXECL

解决方案 »

  1.   

    参考一下吧 
    procedure TForm12.BitBtn3Click(Sender: TObject);
    var
    i,j:integer;
    begin
    excelapplication1.Connect;
    excelapplication1.Visible[0]:=true;
    excelapplication1.Workbooks.Add(null,0);
    excelworkbook1.ConnectTo(excelapplication1.Workbooks[1]);
    excelworksheet1.ConnectTo(excelworkbook1.Sheets[1] as _worksheet);
     if checkbox3.Checked then
         begin
           excelworksheet1.Cells.Item[1,1]:='车牌号码';
           excelworksheet1.Cells.Item[1,2]:='车辆说明';
           excelworksheet1.Cells.Item[1,3]:='加油时间';
           excelworksheet1.Cells.Item[1,4]:='加油地点';
           excelworksheet1.Cells.Item[1,5]:='加油类型';
           excelworksheet1.Cells.Item[1,6]:='加油数量';
           excelworksheet1.Cells.Item[1,7]:='单价';
           excelworksheet1.Cells.Item[1,8]:='金额';
           excelworksheet1.Cells.Item[1,9]:='备注';
           i:=2;
           while not datamodule4.ADOQuery1.Eof do
                begin
                excelworksheet1.Cells.Item[i,1]:=datamodule4.ADOQuery1.fieldbyname('车牌号码').AsString;
                excelworksheet1.Cells.Item[i,2]:=datamodule4.ADOQuery1.fieldbyname('车辆说明').AsString;
                excelworksheet1.Cells.Item[i,3]:=datamodule4.ADOQuery1.fieldbyname('加油时间').AsString;
                excelworksheet1.Cells.Item[i,4]:=datamodule4.ADOQuery1.fieldbyname('加油地点').AsString;
                excelworksheet1.Cells.Item[i,5]:=datamodule4.ADOQuery1.fieldbyname('加油类型').AsString;
                excelworksheet1.Cells.Item[i,6]:=datamodule4.ADOQuery1.fieldbyname('加油数量').AsString;
                excelworksheet1.Cells.Item[i,7]:=datamodule4.ADOQuery1.fieldbyname('单价').AsString;
                excelworksheet1.Cells.Item[i,8]:=datamodule4.ADOQuery1.fieldbyname('金额').AsString;
                excelworksheet1.Cells.Item[i,9]:=datamodule4.ADOQuery1.fieldbyname('备注').AsString;
                i:=i+1;
                datamodule4.ADOQuery1.Next;
                end;
           end;