下面的过程是将数据库中的记录写到EXCEL文件中.var
   XL, XArr: Variant;
   i : Integer;
   j : Integer;
begin
     {note the ComObj (example OleAuto not correct) in the uses}
     // Create an array of query element size
     XArr:=VarArrayCreate([1,EmailQuery.FieldCount],varVariant);
     XL:=CreateOLEObject('Excel.Application');        
     XL.WorkBooks.add;
     XL.visible:=true;
     
     j := 1;
     EmailQuery.First;
     while not EmailQuery.Eof do begin
           i:=1;
           while i<=EmailQuery.FieldCount do begin
                 XArr[i] := EmailQuery.Fields[i-1].Value;
                 i := i+1;
           end;
           XL.Range['A'+IntToStr(j),
           CHR(64+EmailQuery.FieldCount)+IntToStr(j)].Value := XArr;
           EmailQuery.Next;
           j := j + 1;
     end;
     XL.Range['A1',CHR(64+EmailQuery.FieldCount)+IntToStr(j)].select;
     // XL.cells.select;                     // Select everything
     XL.Selection.Font.Name:='Garamond';
     XL.Selection.Font.Size:=10;
     XL.selection.Columns.AutoFit;
     XL.Range['A1','A1'].select;
end;

解决方案 »

  1.   

    odbc 的可以参考
    http://www.csdn.net/develop/Article/14/14926.shtmExcel文件也是可以直接读的。
    var
        MSExcel:                       Variant;       //*
    MSExcel:=CreateOLEObject('Excel.Application'); 
    MSExcel.WorkBooks.Open('*.xsl');                 //文件名a:=MSExcel.ActiveSheet.UsedRange.Rows.Count;    //得到总行数
    b:=MSExcel.ActiveSheet.UsedRange.column.Count   //列数
    for i:=1 to a do
     for j:= 1 to b do
     begin
       MSExcel.Cells[i,j].Value     //得到值
     end;

    MSExcel.ActiveWorkBook.Close;  //关闭
    MSExcel.Quit;
      

  2.   

    写EXCEL
    procedure TForm1.Button1Click(Sender: TObject);var
    i,row,column:integer;
    begin
       try
           ExcelApplication1.Connect ;   except
           MessageDlg ('Excel may not be installed ',mtError,[mbOk],0);
           Abort ; 
       end;
       ExcelApplication1.Visible [0]:=true;
       ExcelApplication1.Caption :='Excel Application';
       ExcelApplication1.Workbooks.Add(null,0);
       ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);  //连接工作薄
       ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _worksheet); //连接一个单元
       ADODataSet1.Open ;
       row:=2;
       while not ADODataSet1.Eof do
       begin
         column :=1;
         for  i:=1 to ADODataSet1.FieldCount do
         begin
            ExcelWorksheet1.Cells.Item[row,column]:=ADODataSet1.Fields[i-1].AsString;
            column:=column+1;
         end;
          ADODataSet1.Next;
          row:=row+1;
       end;
         ExcelApplication1.Free;
         ExcelWorkbook1.Free;
         ExcelWorksheet1.Free;  
    end;