unit DetDBExport;
interface
uses Windows, Classes, FileCtrl, SysUtils, DBGrids,
DB, DBTables;
procedure ExportDBGridToCSV(TheGrid: TDBGrid;
ShowFieldNames: Boolean;
const FileName, Trenner:
string);
overload;
procedure ExportDBGridToCSV(TheGrid: TDBGrid;
ShowFieldNames: Boolean;
const FileName, Trenner:
string;
QuoteStrings: Boolean);
overload;
procedure ExportDBGridToCSV(TheGrid: TDBGrid;
ShowFieldNames: Boolean;
const FileName, Trenner:
string;
QuoteStrings: Boolean;
DeleteLastLimiter: Boolean);
overload;
procedure ExportDBGridToCSV(TheGrid: TDBGrid;
ShowFieldNames: Boolean;
const FileName, Trenner:
string;
QuoteStrings: Boolean;
DeleteLastLimiter: Boolean;
ForceDestDir: Boolean);
overload;
procedure ExportDBGridToCSV(TheGrid: TDBGrid;
ShowFieldNames: Boolean;
const FileName, Trenner:
string;
QuoteStrings: Boolean;
DeleteLastLimiter: Boolean;
ForceDestDir: Boolean;
AskUser: Boolean);
overload;
procedure ExportDBGridToXML(TheGrid: TDBGrid;
const FileName, DataDescription:
string);
implementation
function GetNiceName(
const sDir:
string):
string;
begin
Result := '
';
if Length(sDir) = 0
then exit;
if AnsiLastChar(sDir) = '
\'
then
Result := Copy(sDir,1,Length(sDir) - 1)
else
Result := sDir;
end;
procedure ExportDBGridToCSV(TheGrid: TDBGrid;
ShowFieldNames: Boolean;
const FileName, Trenner:
string);
var i, j: integer;
Liste: TStringList;
Zeile, Feld:
string;
begin
if not(Assigned(TheGrid)
and Assigned(TheGrid.DataSource))
then exit;
Liste := TStringList.Create;
with TheGrid.DataSource.DataSet
do
try
First;
if ShowFieldNames
then
begin
for j := 0
to TheGrid.FieldCount - 1
do
begin
Feld := Fields[j].FieldName;
Zeile := Zeile + Feld + Trenner;
end;
Liste.Add(Zeile);
end;
for i := 0
to RecordCount - 1
do
begin
Zeile := '
';
for j := 0
to TheGrid.FieldCount - 1
do
begin
Feld := Fields[j].AsString;
Zeile := Zeile + Feld + Trenner;
end;
Liste.Add(Zeile);
Next;
end;
finally
Liste.SaveToFile(FileName);
Liste.Free;
end;
end;
procedure ExportDBGridToCSV(TheGrid: TDBGrid;
ShowFieldNames: Boolean;
const FileName, Trenner:
string;
QuoteStrings: Boolean);
var i, j: integer;
Liste: TStringList;
Zeile, Feld:
string;
begin
if not(Assigned(TheGrid)
and Assigned(TheGrid.DataSource))
then exit;
Liste := TStringList.Create;
with TheGrid.DataSource.DataSet
do
try
First;
if ShowFieldNames
then
begin
for j := 0
to TheGrid.FieldCount - 1
do
begin
Feld := Fields[j].FieldName;
Zeile := Zeile + Feld + Trenner;
end;
Liste.Add(Zeile);
end;
for i := 0
to RecordCount - 1
do
begin
Zeile := '
';
for j := 0
to TheGrid.FieldCount - 1
do
begin
Feld := Fields[j].AsString;
if QuoteStrings
and (Fields[j]
is TStringField)
then
Feld := '
"' + Feld + '
"';
Zeile := Zeile + Feld + Trenner;
end;
Liste.Add(Zeile);
Next;
end;
finally
Liste.SaveToFile(FileName);
Liste.Free;
end;
end;
procedure ExportDBGridToCSV(TheGrid: TDBGrid;
ShowFieldNames: Boolean;
const FileName, Trenner:
string;
QuoteStrings: Boolean;
DeleteLastLimiter: Boolean);
var i, j: integer;
Liste: TStringList;
Zeile, Feld:
string;
begin
if not(Assigned(TheGrid)
and Assigned(TheGrid.DataSource))
then exit;
Liste := TStringList.Create;
with TheGrid.DataSource.DataSet
do
try
First;
if ShowFieldNames
then
begin
for j := 0
to TheGrid.FieldCount - 1
do
begin
Feld := Fields[j].FieldName;
Zeile := Zeile + Feld + Trenner;
end;
Liste.Add(Zeile);
end;
for i := 0
to RecordCount - 1
do
begin
Zeile := '
';
for j := 0
to TheGrid.FieldCount - 1
do
begin
Feld := Fields[j].AsString;
if QuoteStrings
and (Fields[j]
is TStringField)
then
Feld := '
"' + Feld + '
"';
Zeile := Zeile + Feld + Trenner;
end;
if DeleteLastLimiter
then
System.Delete(Zeile,Length(Zeile) - Pred(Length(Trenner)),Length(Zeile));
Liste.Add(Zeile);
Next;
end;
finally
Liste.SaveToFile(FileName);
Liste.Free;
end;
end;
procedure ExportDBGridToCSV(TheGrid: TDBGrid;
ShowFieldNames: Boolean;
const FileName, Trenner:
string;
QuoteStrings: Boolean;
DeleteLastLimiter: Boolean;
ForceDestDir: Boolean);
var i, j: integer;
Liste: TStringList;
Zeile, Feld:
string;
begin
if not(Assigned(TheGrid)
and Assigned(TheGrid.DataSource))
then exit;
if not DirectoryExists(ExtractFilePath(FileName))
then
begin
if ForceDestDir
then
begin
ForceDirectories(ExtractFilePath(FileName));
if not DirectoryExists(ExtractFilePath(FileName))
then
begin
MessageBox(0,
PChar(Format('
Das Verzeichnis %s konnte nicht angelegt'
+#13#10+'
werden.',
[GetNiceName(ExtractFilePath(FileName))])),
'
Fehler',
MB_OK
or MB_ICONERROR);
exit;
end;
end
else
begin
MessageBox(0,
PChar(Format('
Das Verzeichnis %s existiert nicht.',
[GetNiceName(ExtractFilePath(FileName))])),
'
Fehler',
MB_OK
or MB_ICONERROR);
exit;
end;
end;
Liste := TStringList.Create;
with TheGrid.DataSource.DataSet
do
try
First;
if ShowFieldNames
then
begin
for j := 0
to TheGrid.FieldCount - 1
do
begin
Feld := Fields[j].FieldName;
Zeile := Zeile + Feld + Trenner;
end;
Liste.Add(Zeile);
end;
for i := 0
to RecordCount - 1
do
begin
Zeile := '
';
for j := 0
to TheGrid.FieldCount - 1
do
begin
Feld := Fields[j].AsString;
if QuoteStrings
and (Fields[j]
is TStringField)
then
Feld := '
"' + Feld + '
"';
Zeile := Zeile + Feld + Trenner;
end;
if DeleteLastLimiter
then
System.Delete(Zeile,Length(Zeile) - Pred(Length(Trenner)),Length(Zeile));
Liste.Add(Zeile);
Next;
end;
finally
Liste.SaveToFile(FileName);
Liste.Free;
end;
end;
procedure ExportDBGridToCSV(TheGrid: TDBGrid;
ShowFieldNames: Boolean;
const FileName, Trenner:
string;
QuoteStrings: Boolean;
DeleteLastLimiter: Boolean;
ForceDestDir: Boolean;
AskUser: Boolean);
var i, j: integer;
Liste: TStringList;
Zeile, Feld:
string;
begin
if not(Assigned(TheGrid)
and Assigned(TheGrid.DataSource))
then exit;
if not DirectoryExists(ExtractFilePath(FileName))
then
begin
if (MessageBox(0,
PChar(Format('
Das Verzeichnis %s existiert nicht.' +#13#10+
'
Soll es angelegt werden?',
[GetNiceName(ExtractFilePath(FileName))])),
'
Verzeichnis nicht gefunden',
MB_OKCANCEL
or MB_ICONQUESTION) = idOK)
and ForceDestDir
then
begin
ForceDirectories(ExtractFilePath(FileName));
if not DirectoryExists(ExtractFilePath(FileName))
then
begin
MessageBox(0,
PChar(Format('
Das Verzeichnis %s konnte nicht' +
'
angelegt werden.',
[GetNiceName(ExtractFilePath(FileName))])),
'
Fehler',
MB_OK
or MB_ICONERROR);
exit;
end;
end
else
begin
if not ForceDestDir
then
MessageBox(0,
PChar(Format('
Das Verzeichnis %s existiert nicht.',
[GetNiceName(ExtractFilePath(FileName))])),
'
Fehler',
MB_OK
or MB_ICONERROR);
exit;
end;
end;
Liste := TStringList.Create;
with TheGrid.DataSource.DataSet
do
try
if ShowFieldNames
then
begin
for j := 0
to TheGrid.FieldCount - 1
do
begin
Feld := Fields[j].FieldName;
Zeile := Zeile + Feld + Trenner;
end;
Liste.Add(Zeile);
end;
First;
for i := 0
to RecordCount - 1
do
begin
Zeile := '
';
for j := 0
to TheGrid.FieldCount - 1
do
begin
Feld := Fields[j].AsString;
if QuoteStrings
and (Fields[j]
is TStringField)
then
Feld := '
"' + Feld + '
"';
Zeile := Zeile + Feld + Trenner;
end;
if DeleteLastLimiter
then
System.Delete(Zeile,Length(Zeile) - Pred(Length(Trenner)),Length(Zeile));
Liste.Add(Zeile);
Next;
end;
finally
Liste.SaveToFile(FileName);
Liste.Free;
end;
end;
procedure ExportDBGridToXML(TheGrid: TDBGrid;
const FileName, DataDescription:
string);
var i, j: integer;
Liste: TStringList;
Zeile, Feld, Desc:
string;
begin
if not(Assigned(TheGrid)
and Assigned(TheGrid.DataSource))
then exit;
if Length(trim(DataDescription)) < 1
then exit;
Liste := TStringList.Create;
Liste.Add('
<?xml version="1.0" encoding="ISO-8859-1" ?>');
try
Desc := TDBDataSet(TheGrid.DataSource.DataSet).DataBaseName;
except
Desc := '
Datensatz';
end;
Liste.Add(Format('
<%s>',[Desc]));
with TheGrid.DataSource.DataSet
do
try
First;
for i := 0
to RecordCount - 1
do
begin
Liste.Add(Format('
<%s>',[DataDescription]));
for j := 0
to TheGrid.FieldCount - 1
do
begin
Zeile := Format('
<%s>',[Fields[j].FieldName]);
Feld := Fields[j].AsString;
Feld := StringReplace(Feld,'
&','
&',[rfReplaceAll]);
Feld := StringReplace(Feld,'
>','
>',[rfReplaceAll]);
Feld := StringReplace(Feld,'
<','
<',[rfReplaceAll]);
Feld := StringReplace(Feld,'
"','
"',[rfReplaceAll]);
Feld := StringReplace(Feld,'
''
','
'',[rfReplaceAll]);
Zeile := Zeile + Feld + Format('
</%s>',[Fields[j].FieldName]);
Liste.Add(Zeile);
end;
Liste.Add(Format('
</%s>',[DataDescription]));
Next;
end;
Liste.Add(Format('
</%s>',[Desc]));
finally
Liste.SaveToFile(FileName);
Liste.Free;
end;
end;
end.