最好是范例啊~~谢谢~~~~

解决方案 »

  1.   

    procedure TfrmGlobal.DBGridInFoToExcel(FileName, TitleCaption: string;
      MakeDataSource: TDataSource; makeDBGrid: TDBGrid);
    var
         xlApp, xlSheet, szValue: Variant;
         ARow, iLoop: word;
    begin
         xlApp := CreateOleObject('Excel.Application');
         try
             xlSheet := CreateOleObject('Excel.Sheet');
             xlSheet := xlApp.WorkBooks.Add;
             
             // 主标题
              xlSheet.WorkSheets[1].Cells[1,1] := TitleCaption;
             
        //  表格标题     
             for iLoop := 0 to makeDBGridEh.Columns.Count - 1 do
                  xlSheet.WorkSheets[1].Cells[2, iLoop+1] := makeDBGridEh.Columns[iLoop].Title.Caption;         // 数据
             ARow := 3;
             with MakeDataSource.DataSet do
             begin
                  DisableControls;
                  First;
                  while not Eof do
                  begin
                       for iLoop := 0 to Fields.Count - 1 do
                       begin
                           szValue := Fields[iLoop].Value;
                           xlSheet.WorkSheets[1].Cells[ARow, iLoop+1] := szValue;
                       end;
                       Inc(ARow);
                       Next;
                  end;
                  First;
                  EnableControls;
             end;         try
                  xlSheet.SaveAs(FileName);
                  Application.MessageBox('导出完毕!', '提示', MB_IconExclamation);
             finally
                  xlSheet.Close;
                  xlApp.Quit;
                  xlApp := UnAssigned;
             end;
         except
              MessageBox(handle, '本机没有安装Excel.', '提示',MB_IconExclamation);
         end;end;
      

  2.   

    呵呵,找找有没有这样的控件;其实你可以在调用它时用一个InputBox来取一个文件名传入过程的FileName啊
      

  3.   

    procedure TW_Kqrb.SpeedButton12Click(Sender: TObject);
    var
     i,row:integer;
     j:Int64;
     Excel:variant;
    begin
     If (Query1.Active = False) or (Query1.RecordCount = 0) Then Exit; Excel:=UnAssigned;
     Try
       Excel:=CreateOleObject('Excel.Application');
     Except
      Application.MessageBox('Excel应用程序没有找到,系统不能将数据导入Excel!','错误',MB_OK + MB_ICONERROR);
      Exit;
     End;
     Excel.Visible:=True;
     Excel.DisplayAlerts:=false;
     Excel.WorkBooks.Add;
    // RefreshInfo;
     SetForegroundWindow(Handle);
     Excel.Caption:='考勤日报';
     Excel.Cells[1,1]:=P_gsid;
     Excel.Cells[2,1]:='考勤日报';
     For I:=0 to DbGrid1.Columns.Count - 1 do
    // ExcelWorksheet1.Cells.Item[5,I+1]:=DbGrid1.Columns[I].Title.Caption;
     Excel.Cells[5,I+1]:=DbGrid1.Columns[I].Title.Caption;
     Row:=6;
     Query1.First;
     While Not Query1.Eof do
     begin
       Application.ProcessMessages;
       For I:=0 to DbGrid1.Columns.Count - 1 do
         Excel.Cells[Row,I+1]:=''''+Query1.FieldByName(DbGrid1.Columns[I].FieldName).AsString;
       Query1.Next;
       Row:=Row+1;
     end;
    end;
      

  4.   

    下载个dx的TDxDBgrid就有SaveToXLS方法,很方便,没装excel也可以导为xls文件