如何将数据库数据导成 (CSV) 格式的文件(50分)

  • 主题发起人 主题发起人 chenglc
  • 开始时间 开始时间
C

chenglc

Unregistered / Unconfirmed
GUEST, unregistred user!
在delphi 中,如何把查询的数据输出成(CSV)格式的文件.请帮忙解决!!!
 
怎么没人回答啊?
 
去看DEMO```Delphi中自带例子
 
先去分析一下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.Title.Caption;
if i <> ColumnsList.Count - 1 then
s := s + Separator;
StreamWriteString(Stream, s);
// Stream.Write(PChar(s)^, Length(s));
end;
end;
 
CSV就是逗号格式,简单的做法就是你自己打开一个文本文件,使用循环把每个记录的每个字段写入文本文件,字段直接使用','分格,每个记录完成加上回车#13提行。
 
后退
顶部