1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
{*****************************************}
{ }
{ FastReport v2.3 }
{ CSV export filter }
{ }
{ Copyright (c) 1998-99 by Tzyganenko A. }
{ }
{*****************************************}
unit LR_E_CSV;
interface
{$I lr_vers.inc}
uses
Classes, SysUtils, LResources, LazUTF8,
Graphics,GraphType, Controls, Forms, Dialogs,LR_E_TXT,
LCLType,LCLIntf,lr_utils,lr_class;
type
TfrQuoteType = (qtNone, qtQuoteChar, qtAutoQuote);
TfrCSVExport = class(TComponent)
public
Constructor Create(aOwner : TComponent); override;
end;
{ TfrCSVExportFilter }
TfrCSVExportFilter = class(TfrTextExportFilter)
private
FQuoteChar: TUTF8Char;
FQuoteType: TfrQuoteType;
FSeparator: TUTF8Char;
protected
procedure GetUsedFont; override;
procedure CalcXCoords(var {%H-}x,{%H-}w: integer); override;
public
constructor Create(AStream: TStream); override;
procedure OnEndPage; override;
procedure OnData(x, y: Integer; View: TfrView); override;
procedure OnText({%H-}X, {%H-}Y: Integer; const {%H-}Text: String; {%H-}View: TfrView); override;
property QuoteChar: TUTF8Char read FQuoteChar write FQuoteChar;
property QuoteType: TfrQuoteType read FQuoteType write FQuoteType;
property Separator: TUTF8Char read FSeparator write FSeparator;
end;
implementation
uses LR_Const;
procedure TfrCSVExportFilter.GetUsedFont;
begin
// never ask usedfont dialog in CSV exporter
end;
procedure TfrCSVExportFilter.CalcXCoords(var x, w: integer);
begin
// do not convert x coords in CSV exporter
end;
constructor TfrCSVExportFilter.Create(AStream: TStream);
begin
inherited Create(AStream);
FQuoteType := qtQuoteChar;
FQuoteChar := '"';
FSeparator := ',';
BandTypes := [btMasterHeader,btMasterData,btColumnHeader];
end;
function CompareIntervals(Item1, Item2: Pointer): Integer;
begin
result := {%H-}PtrInt(Item1)-{%H-}PtrInt(Item2);
end;
procedure TfrCSVExportFilter.OnEndPage;
var
i,n: Integer;
p: PfrTextRec;
s: String;
procedure AddStr(aStr: string);
var
Qt: TfrQuoteType;
begin
Qt := QuoteType;
if Qt=qtAutoQuote then begin
if pos(Separator, aStr)<>0 then
Qt := qtQuoteChar
else
Qt := qtNone;
end;
if Qt=qtNone then begin
if s = '' then
s := aStr
else
s := s + Separator + aStr;
end else
begin
if s = '' then
s := UTF8Quotedstr(aStr, QuoteChar)
else
s := s + Separator + UTF8Quotedstr(aStr, QuoteChar);
end;
end;
begin
n := Lines.Count - 1;
while n >= 0 do
begin
if Lines[n] <> nil then break;
Dec(n);
end;
for i := 0 to n do
begin
s := '';
p := PfrTextRec(Lines[i]);
while p<>nil do begin
if P^.Typ in [gtMemo,gtAddin] then
AddStr(P^.Text);
p := p^.Next;
end;
s := s + LineEnding;
Stream.Write(s[1], Length(s));
end;
end;
procedure TfrCSVExportFilter.OnData(x, y: Integer; View: TfrView);
begin
AddData(x, y, View);
end;
procedure TfrCSVExportFilter.OnText(X, Y: Integer; const Text: String;
View: TfrView);
begin
//
end;
constructor TfrCSVExport.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
frRegisterExportFilter(TfrCSVExportFilter, sCSVFile + ' (*.csv)', '*.csv');
end;
end.
|