fpcsvexport.pp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. unit fpcsvexport;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpDBExport, csvreadwrite;
  6. Type
  7. { TCSVFormatSettings }
  8. TCSVFormatSettings = Class(TExportFormatSettings)
  9. Private
  10. FDelimiter: String;
  11. FHeaderRow: Boolean;
  12. FIgnoreOuterWhiteSpace: Boolean;
  13. FRowDelimiter: String;
  14. FQuoteChar: Char;
  15. Public
  16. Constructor Create(DoInitSettings : Boolean); override;
  17. Procedure Assign(Source : TPersistent); override;
  18. // Kept for compatibility with older versions; please replace with QuoteChar
  19. Property StringQuoteChar : Char Read FQuoteChar Write FQuoteChar; deprecated 'Please replace with QuoteChar';
  20. Published
  21. // Properties
  22. // Delimiter between fields/columns. Traditionally , for CSV.
  23. Property FieldDelimiter : String Read FDelimiter Write FDelimiter;
  24. //If no, CSV is RFC 4180 compliant; if yes, it matches the unofficial Creativyst specification
  25. Property IgnoreOuterWhitespace : Boolean Read FIgnoreOuterWhiteSpace write FIgnoreOuterWhiteSpace;
  26. // Line ending to be used between rows of data (e.g. #13#10 for standard CSV)
  27. Property RowDelimiter : String Read FRowDelimiter Write FRowDelimiter;
  28. // Whether or not the file should have a header row with field names
  29. Property HeaderRow : Boolean Read FHeaderRow Write FHeaderRow default true;
  30. // If fields need to be surrounded by quotes, use this character (e.g. ")
  31. Property QuoteChar : Char Read FQuoteChar Write FQuoteChar;
  32. end;
  33. { TCustomCSVExporter }
  34. TCustomCSVExporter = Class(TCustomFileExporter)
  35. private
  36. FCSVOut: TCSVBuilder;
  37. function GetCSVFormatsettings: TCSVFormatSettings;
  38. procedure SetCSVFormatSettings(const AValue: TCSVFormatSettings);
  39. Protected
  40. Function CreateFormatSettings : TCustomExportFormatSettings; override;
  41. Procedure DoBeforeExecute; override;
  42. Procedure DoAfterExecute; override;
  43. Procedure DoDataHeader; override;
  44. Procedure ExportField(EF : TExportFieldItem); override;
  45. Procedure DoDataRowEnd; override;
  46. Public
  47. Constructor Create(Aowner : TComponent); override;
  48. Property FormatSettings : TCSVFormatSettings Read GetCSVFormatsettings Write SetCSVFormatSettings;
  49. end;
  50. { TCSVExporter }
  51. TCSVExporter = Class(TCustomCSVExporter)
  52. Published
  53. Property FileName;
  54. Property Dataset;
  55. Property ExportFields;
  56. Property FromCurrent;
  57. Property RestorePosition;
  58. Property FormatSettings;
  59. Property OnExportRow;
  60. end;
  61. Procedure RegisterCSVExportFormat;
  62. Procedure UnRegisterCSVExportFormat;
  63. Const
  64. SCSVExport = 'CSV';
  65. SCSVExtensions = '.csv;.txt';
  66. ResourceString
  67. SCSVDescription = 'Comma-Separated Values (CSV)';
  68. implementation
  69. { TCustomCSVExporter }
  70. procedure TCustomCSVExporter.DoBeforeExecute;
  71. begin
  72. inherited DoBeforeExecute;
  73. FCSVOut:=TCSVBuilder.Create;
  74. if (FormatSettings.FieldDelimiter<>'') then
  75. FCSVOut.Delimiter:=FormatSettings.FieldDelimiter[1];
  76. FCSVOut.IgnoreOuterWhitespace:=FormatSettings.IgnoreOuterWhitespace;
  77. FCSVOut.LineEnding:=FormatSettings.RowDelimiter;
  78. FCSVOut.QuoteChar:=FormatSettings.QuoteChar;
  79. OpenTextFile;
  80. FCSVOut.SetOutput(Stream); //output to the export stream
  81. end;
  82. procedure TCustomCSVExporter.DoAfterExecute;
  83. begin
  84. FCSVOut.Free;
  85. CloseTextFile;
  86. inherited DoAfterExecute;
  87. end;
  88. function TCustomCSVExporter.GetCSVFormatsettings: TCSVFormatSettings;
  89. begin
  90. Result:=TCSVFormatSettings(Inherited FormatSettings)
  91. end;
  92. procedure TCustomCSVExporter.SetCSVFormatSettings(
  93. const AValue: TCSVFormatSettings);
  94. begin
  95. Inherited FormatSettings:=AValue;
  96. end;
  97. function TCustomCSVExporter.CreateFormatSettings: TCustomExportFormatSettings;
  98. begin
  99. Result:=TCSVFormatSettings.Create(False)
  100. end;
  101. procedure TCustomCSVExporter.DoDataHeader;
  102. Var
  103. I : Integer;
  104. begin
  105. If FormatSettings.HeaderRow then
  106. begin
  107. For I:=0 to ExportFields.Count-1 do
  108. if ExportFields[i].Enabled then
  109. FCSVOut.AppendCell(ExportFields[i].ExportedName);
  110. FCSVOut.AppendRow; //close off with line ending
  111. end;
  112. inherited DoDataHeader;
  113. end;
  114. procedure TCustomCSVExporter.ExportField(EF: TExportFieldItem);
  115. begin
  116. FCSVOut.AppendCell(FormatField(EF.Field));
  117. end;
  118. procedure TCustomCSVExporter.DoDataRowEnd;
  119. begin
  120. FCSVOut.AppendRow; //Line ending
  121. end;
  122. constructor TCustomCSVExporter.Create(Aowner: TComponent);
  123. begin
  124. inherited Create(Aowner);
  125. end;
  126. { TCSVFormatSettings }
  127. constructor TCSVFormatSettings.Create(DoInitSettings: Boolean);
  128. begin
  129. // These defaults are meant to be Excel CSV compatible
  130. inherited Create(DoInitSettings);
  131. FHeaderRow:=True;
  132. FDelimiter:=',';
  133. FQuoteChar:='"';
  134. FRowDelimiter:=LineEnding;
  135. end;
  136. procedure TCSVFormatSettings.Assign(Source: TPersistent);
  137. Var
  138. FS : TCSVFormatsettings;
  139. begin
  140. If (Source is TCSVFormatSettings) then
  141. begin
  142. FS:=Source as TCSVFormatSettings;
  143. FDelimiter:=FS.FDelimiter;
  144. FHeaderRow:=FS.FHeaderRow;
  145. FRowDelimiter:=FS.FRowDelimiter;
  146. FQuoteChar:=FS.FQuoteChar;
  147. end;
  148. inherited Assign(Source);
  149. end;
  150. Procedure RegisterCSVExportFormat;
  151. begin
  152. ExportFormats.RegisterExportFormat(SCSVExport,SCSVDescription,SCSVExtensions,TCSVExporter);
  153. end;
  154. Procedure UnRegisterCSVExportFormat;
  155. begin
  156. ExportFormats.UnRegisterExportFormat(SCSVExport);
  157. end;
  158. end.