2013-01-18 284 views
2

我有一個數據庫網格被排序(用戶點擊幾個單選按鈕和複選框來影響顯示)。將DBgrid導出爲CSV?

我想導出所有數據(不僅僅是網格中可見的數據),按照相同的順序導出爲CSV - 我該怎麼做?數據 - 不是用戶設置,只是爲了澄清。

預先感謝任何幫助


[更新]我建sqlQuery的點點滴滴,這取決於複選框的用戶設置&單選按鈕組,然後,當他們中的一個變化,我

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

也就是說,沒有硬編碼查詢,它會變化,我想導出當前設置。

我不知道如果我想從網格或數據集(我只是不是數據庫的傢伙,這是我的第一個DBgrid)導出,但我懷疑我想要網格,因爲它有一個他數據集的字段子集。

我猜TJvDBGridCSVExport是一個Jedi組件(?)我試圖避免它們到目前爲止,聽起來很棒,因爲我更喜歡謹慎的,獨立的組件來安裝一個巨大的集合。這可能不是做最聰明的事情,但它是我的感覺 - YMMV(和prolly做)

+1

試過TJvDBGridCSVExport? –

+1

數據的執行順序如何?相關的數據集應該具有與網格所示相同的順序,並且您只需將數據集導出爲CSV –

+2

@ Arioch'TJvDBGridCSVExport是從相關DataSet中導出所有數據還是僅導出Grid所顯示的數據? –

回答

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

你可以使用自己的小程序至極可以適應您的需求

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;