程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 關於DBGRIDEH導出數據到CSV

關於DBGRIDEH導出數據到CSV

編輯:Delphi
在通常情況下使用DBGRIDEH導出的到CSV中的數據是這個樣子的

  "a","b","c"

  可能我們並不希望它這樣顯示,有可能希望它顯示成種狀態

  a,b,c

  如果想這樣,我們可以修改DBGRIDEH裡面的DBGridEhImpExp.pas文件

  具體修改如下:增加一個自己的導出到CSV的類

   { TMyDBGridEhExportAsCVS }
  
    TMyDBGridEhExportAsCVS = class(TDBGridEhExportAsText)
    private
      FSeparator: Char;
    protected
      procedure CheckFirstCell; override;
      procedure WriteTitle(ColumnsList: TColumnsEhList); override;
      procedure WriteDataCell(Column: TColumnEh; FColCellParamsEh: TColCellParamsEh); override;
      procedure WriteFooterCell(DataCol, Row: Integer; Column: TColumnEh; AFont: TFont;
        Background: TColor; Alignment: TAlignment; Text: String); override;
    public
      constructor Create; override;
      property Separator: Char read FSeparator write FSeparator;
    end;
  
  { TMyDBGridEhExportAsCVS }
  
  procedure TMyDBGridEhExportAsCVS.CheckFirstCell;
  var s: String;
  begin
    if FirstCell = False then
    begin
      s := Separator;
      StreamWriteString(Stream, s);
  //    Stream.Write(PChar(s)^, Length(s))
    end else
      FirstCell := False;
  end;
  
  constructor TMyDBGridEhExportAsCVS.Create;
  begin
    Separator := ',';
    inherited Create;
  end;
  
  procedure TMyDBGridEhExportAsCVS.WriteDataCell(Column: TColumnEh; FColCellParamsEh: TColCellParamsEh);
  var s: String;
  begin
    CheckFirstCell;
    s := FColCellParamsEh.Text;
    StreamWriteString(Stream, s);
  //  Stream.Write(PChar(s)^, Length(s));
  end;
  
  procedure TMyDBGridEhExportAsCVS.WriteFooterCell(DataCol, Row: Integer;
    Column: TColumnEh; AFont: TFont; Background: TColor;
    Alignment: TAlignment; Text: String);
  var s: String;
  begin
    CheckFirstCell;
    s := Text;
    StreamWriteString(Stream, s);
  //  Stream.Write(PChar(s)^, Length(s));
  end;
  
  procedure TMyDBGridEhExportAsCVS.WriteTitle(ColumnsList: TColumnsEhList);
  var i: Integer;
    s: String;
  begin
    CheckFirstRec;
    for i := 0 to ColumnsList.Count - 1 do
    begin
      s := ColumnsList[i].Title.Caption;
      if i <> ColumnsList.Count - 1 then
        s := s + Separator;
      StreamWriteString(Stream, s);
  //    Stream.Write(PChar(s)^, Length(s));
    end;
  end;

  

   

  Good luck!

   

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved