将DBgrid导出为CSV?

时间:2013-01-18 08:06:58

标签: delphi export-to-csv tdbgrid

我有一个已排序的数据库网格(用户单击了几个单选按钮和复选框以影响显示)。

我想将所有数据(不仅仅是网格中可见的数据)以相同的方式导出到CSV - 我该怎么做?数据 - 而不是用户设置,只是为了澄清。

提前感谢您提供任何帮助


[更新]我逐位构建sqlQuery,具体取决于用户对复选框和设置的设置。然后,当其中一个人改变时,无线电组,我

   ActivityADQuery.SQL.Clear();
   ActivityADQuery.SQL.Add(sqlQuery);
   ActivityADQuery.Open(sqlQuery);

也就是说没有硬编码查询,它会有所不同,我想导出当前设置。

我不知道是否要从网格或数据集导出(我只是不是数据库人,这是我的第一个DBgrid),但我怀疑我想要网格,因为它有一个子集他的数据集领域。

我想TJvDBGridCSVExport是一个Jedi组件(?)我到目前为止试图避开它们,听起来很棒,因为我更喜欢谨慎,独立的组件来安装一个巨大的集合。这可能不是最聪明的事情,但这就是我的感受 - ymmv(并且很多)

2 个答案:

答案 0 :(得分:3)

另一种解决方案也适用于(多个)选定的行:

procedure TReportsForm.ExportToCSV(const aGrid : TDBGrid; const FileName : String);
Var
  I, J : Integer;
  SavePlace : TBookmark;
  Table : TStrings;
  HeadTable : String;
  LineTable : String;
  First : Boolean;
Begin

  HeadTable := '';
  LineTable := '';
  Table := TStringList.Create;
  First := True;

  Try
    For I := 0 To Pred(aGrid.Columns.Count) Do
      If aGrid.Columns[I].Visible Then
        If First Then
        Begin
// Use the text from the grid, in case it has been set programatically
// E.g., we prefer to show "Date/time" than "from_unixtime(activity.time_stamp, "%D %b %Y  %l:%i:%S")"
//          HeadTable := HeadTable + aGrid.Columns[I].FieldName;
          HeadTable := HeadTable + ActivityReportStringGrid.Columns[i].Title.Caption + ','; // Previous separated wth semi-colon, not comma! (global)
          First := False;
        End
        Else
        begin
//          HeadTable := HeadTable + ';' + aGrid.Columns[I].FieldName;
          HeadTable := HeadTable + ActivityReportStringGrid.Columns[i].Title.Caption + ',';
        end;

    Delete(HeadTable, Length(HeadTable), 1);  // Remove the superfluous trailing comma
    Table.Add(HeadTable);
    First := True;

    // with selection of rows
    If aGrid.SelectedRows.Count > 0 Then
    Begin
      For i := 0 To aGrid.SelectedRows.Count - 1 Do
      Begin
        aGrid.DataSource.Dataset.GotoBookmark(pointer(aGrid.SelectedRows.Items[i]));
        For j := 0 To aGrid.Columns.Count - 1 Do
          If aGrid.Columns[J].Visible Then
            If First Then
            Begin
              lineTable := lineTable + aGrid.Fields[J].AsString;
              First := False;
            End
            Else
              lineTable := lineTable + ',' + aGrid.Fields[J].AsString;

        Delete(LineTable, Length(LineTable), 1);  // Remove the superfluous trailing comma
        Table.Add(LineTable);
        LineTable := '';
        First := True;
      End;
    End
    Else
      //no selection
    Begin
      SavePlace := aGrid.DataSource.Dataset.GetBookmark;
      aGrid.DataSource.Dataset.First;

      Try
        While Not aGrid.DataSource.Dataset.Eof Do
        Begin
          For I := 0 To aGrid.Columns.Count - 1 Do
            If aGrid.Columns[I].Visible Then
              If First Then
              Begin
                lineTable := lineTable + aGrid.Fields[I].AsString;
                First := False;
              End
              Else
                lineTable := lineTable + ',' + aGrid.Fields[I].AsString;


          Delete(LineTable, Length(LineTable), 1);  // Remove the superfluous trailing comma
          Table.Add(LineTable);
          LineTable := '';
          aGrid.DataSource.Dataset.Next;
          First := True;
        End;

        aGrid.DataSource.Dataset.GotoBookmark(SavePlace);
      Finally
        aGrid.DataSource.Dataset.FreeBookmark(SavePlace);
      End;
    End;
    Table.SaveToFile(FileName);
  Finally
    Table.Free;
  End;
End;  // ExportToCSV()

答案 1 :(得分:1)

您可以使用自己的小程序,可以根据您的需要进行调整

Procedure Dataset2SeparatedFile(ads: TDataset; const fn: String; const Separator: String = ';');
var
  sl: TStringList;
  s: String;
  i: Integer;
  bm: TBookmark;

  Procedure ClipIt;
  begin
    s := Copy(s, 1, Length(s) - Length(Separator));
    sl.Add(s);
    s := '';
  end;
  Function FixIt(const s: String): String;
  begin
    // maybe changed
    Result := StringReplace(StringReplace(StringReplace(s, Separator, '', [rfReplaceAll]), #13, '', [rfReplaceAll]), #10, '', [rfReplaceAll]);
    // additional changes could be Quoting Strings
  end;

begin
  sl := TStringList.Create;
  try
    s := '';
    For i := 0 to ads.FieldCount - 1 do
    begin
      if ads.Fields[i].Visible then
        s := s + FixIt(ads.Fields[i].DisplayLabel) + Separator;
    end;
    ClipIt;
    bm := ads.GetBookmark;
    ads.DisableControls;
    try
      ads.First;
      while not ads.Eof do
      begin
        For i := 0 to ads.FieldCount - 1 do
        begin
          if ads.Fields[i].Visible then
            s := s + FixIt(ads.Fields[i].DisplayText) + Separator;
        end;
        ClipIt;
        ads.Next;
      end;
      ads.GotoBookmark(bm);
    finally
      ads.EnableControls;
      ads.FreeBookmark(bm);
    end;
    sl.SaveToFile(fn);
  finally
    sl.Free;
  end;
end;