将数据从DBGrid导出到Excel

将数据从DBGrid导出到Excel,excel,delphi,delphi-7,ado,Excel,Delphi,Delphi 7,Ado,我想知道是否有人提出了一种将数据从DBGrid导出到Excel的方法?我正在使用Delphi 7、Excel 2007和ADO。 任何帮助都将不胜感激 如果您想要快速导出原始数据,只需使用以下内容导出您的记录集(ADODataset.recordset): procedure ExportRecordsetToMSExcel(DestName: string; Data: _Recordset); var ovExcelApp: OleVariant; ovExcelWorkbook:

我想知道是否有人提出了一种将数据从DBGrid导出到Excel的方法?我正在使用Delphi 7、Excel 2007和ADO。

任何帮助都将不胜感激

如果您想要快速导出原始数据,只需使用以下内容导出您的记录集(ADODataset.recordset):

procedure ExportRecordsetToMSExcel(DestName: string; Data: _Recordset);
var
  ovExcelApp: OleVariant;
  ovExcelWorkbook: OleVariant;
  ovWS: OleVariant;
  ovRange: OleVariant;
begin
  ovExcelApp := CreateOleObject('Excel.Application'); //If Excel isnt installed will raise an exception
  try
    ovExcelWorkbook   := ovExcelApp.WorkBooks.Add;
    ovWS := ovExcelWorkbook.Worksheets.Item[1]; // go to first worksheet
    ovWS.Activate;
    ovWS.Select;
    ovRange := ovWS.Range['A1', 'A1']; //go to first cell
    ovRange.Resize[Data.RecordCount, Data.Fields.Count];
    ovRange.CopyFromRecordset(Data, Data.RecordCount, Data.Fields.Count); //this copy the entire recordset to the selected range in excel
    ovWS.SaveAs(DestName, 1, '', '', False, False);
  finally
    ovExcelWorkbook.Close(SaveChanges := False);
    ovWS := Unassigned;
    ovExcelWorkbook := Unassigned;
    ovExcelApp := Unassigned;
  end;
end;

它通过使用Tfilestream组件工作

procedure TForm2.ExportdatatoexcelClick(Sender: TObject);
 var
  Stream: TFileStream;
  i: Integer;
  OutLine,f: string;
  sTemp,s: string;
begin
  Stream := TFileStream.Create('D:\Yogesh Delphi\employee1.csv', fmCreate);
  try
       s := string(adotable1.Fields[0].FieldName);

      for I := 1 to adotable1.FieldCount - 1 do
       begin
        s:= s+ ',' + string(adotable1.Fields[I].FieldName);
       end;
         s:= s+ #13#10;
        stream.Write(s[1], Length(s) * SizeOf(Char));
       {S := '';
      for I := 0 to adotable1.FieldCount - 1 do
        begin
         S := (adotable1.Fields[I].FieldName);
        outline := OutLine+S + ' ,';
        end; }

    while not adotable1.Eof do
    begin
      // You'll need to add your special handling here where OutLine is built
       s:='';
      OutLine := '';
      for i := 0 to adotable1.FieldCount - 1 do
      begin
        sTemp := adotable1.Fields[i].AsString;
        // Special handling to sTemp here
        OutLine := OutLine + sTemp +',';
      end;
      // Remove final unnecessary ','
      SetLength(OutLine, Length(OutLine) - 1);
      // Write line to file
      Stream.Write(OutLine[1], Length(OutLine) * SizeOf(Char));
      // Write line ending
      Stream.Write(sLineBreak, Length(sLineBreak));
      adotable1.Next;
    end;

  finally
    Stream.Free;  // Saves the file
  end;
    showmessage('Records Successfully Exported.') ;
end;
    {Yog}

不要导出DBGrid,导出数据集,只需用字段值填充给定示例中的数据。为了使用变量数组方法,您必须知道查询中有多少行。否则,使用我在那个问题中给出的csv方法可能会更简单。@No'amNewman根据数据类型,csv方法可能会在转换(例如日期时间值)方面带来更多麻烦……谷歌搜索“delphi export dbgrid excel”的点击次数为288000次。我建议您开始使用这些,然后如果您有关于代码部分不起作用的具体问题,请将它们发布在这里。还有bummi的权利;如果可能的话,导出底层数据集。好的,我去看看。谢谢。如果我们想将ClientDataset导出到Excel,那我们该怎么办?如果你没有记录集,你必须迭代你的数据集并编写Excel accesing单元格。例如:ovWS.Cells[RowIndex,ColumnIndex].Value:=Dataset.FieldByName('foo').AsString;