首页 > 学院 > 开发设计 > 正文

关于DBGRIDEH导出数据到CSV

2019-11-18 18:21:19
字体:
来源:转载
供稿:网友

在通常情况下使用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!

 


上一篇:数据完整性(数据的似真性而非正确或者错误)

下一篇:公布TstringGrid增强控件TcbStrGrid源码,带CheckBox的TStringGrid控件

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
学习交流
热门图片

新闻热点

疑难解答

图片精选

网友关注