比如有c:\2.xls, 里只有一个工作表2,然后第一列是标题栏 :
日期,大类,小类,品名,数量,单位,单价,金额
机器装的是office 2000在sql server中有一个stpr数据库,里有一个表是account请问我如何用上面那句句子把account中的全部数据insert到2.xls中?
一开始用过bcp,但是无法在excel文件再插入一个表头
小弟正在学习sql server,以前一直用access(汗...),以前没做过用t-sql语句导出到excel,只是导出少许数据到excel中,但是那个表的数据比较多,水平比较菜,谢谢大家,请务必帮忙在下这个后进,谢谢各位了,我会更加努力的.

解决方案 »

  1.   

    unit toexcel;interface
    uses
      Windows,  Variants, Classes, controls,DB,DBTables, OleServer,Excel2000;procedure ToExce(table1:Tdataset);implementationprocedure ToExce(table1:Tdataset);
    var
    WorkBk : _WorkBook; //  Define a WorkBook
    WorkSheet : _WorkSheet; //  Define a WorkSheet
    I, J, R, C : Integer;
    IIndex : OleVariant;
    TabGrid : Variant;
    XLapp:TexcelApplication;
    begin
       XLapp:=TexcelApplication.create(nil);
       IIndex := 1;
       R := table1.RecordCount;
       C := table1.FieldCount;
       // Create the Variant Array
       TabGrid := VarArrayCreate([0,(R - 1),0,(C - 1)],VarOleStr);   I := 0;
       //  Define the loop for filling in the Variant
        table1.First;
        while not  table1.Eof do
        begin
          for J := 0 to (C - 1) do
           TabGrid[I,J] := table1.Fields[J].Asstring ;
          Inc(I,1);
          table1.Next ;
        end;   // Connect to the server TExcelApplication
       XLApp.Connect;
        // Add WorkBooks to the ExcelApplication
       XLApp.WorkBooks.Add(xlWBatWorkSheet,0);
       // Select the first WorkBook
       WorkBk := XLApp.WorkBooks.Item[IIndex];
       // Define the first WorkSheet
       WorkSheet := WorkBk.WorkSheets.Get_Item(1) as _WorkSheet;
       // Assign the Delphi Variant Matrix to the Variant associated with the WorkSheet
       Worksheet.Range['A1',Worksheet.Cells.Item[R,C]].Value := TabGrid;
       // Customise the WorkSheet
       WorkSheet.Name := 'Customers';
       Worksheet.Columns.Font.Bold := True;
       Worksheet.Columns.HorizontalAlignment := xlRight;
       WorkSheet.Columns.ColumnWidth := 14;
       // Customise the first entire Column
       //WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].Font.Color := clBlue;
       //WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].HorizontalAlignment := xlHAlignLeft;
       //WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].ColumnWidth := 31;
       // Show Excel
       XLApp.Visible[0] := True;
       // Disconnect the Server
       XLApp.Disconnect;
       // Unassign the Delphi Variant Matrix
       TabGrid := Unassigned;
    end;
    end.
      

  2.   

    我不是用控件去导出,而是通过t-sql语句导出,不过还是谢谢你了,我现在己搞定了