help files
Z505 | PasWiki | FUQ | Search | Main Docs | API Guide



Notes

Exporting data as CSV is still useful when one does not wish to export data as XML, or if you want to export your SQL database rows as CSV by iterating through the rows one by one.

To create a CSV file dynamically, you can use the following utilities called csvUtils505. They are located on:

There are many other CSV utilities out there. Stringlists can be used for limited CSV capabilities. The above utilities are meant to be compatible with Excel style CSV format. The above utilities can be used without requiring stringlists or complex classes.

Code example

(Note: we can use any delimiter we want with below functions.. comma is the default since it is Excel compatible)
program CsvDemo; {$ifdef fpc}{$mode objfpc} {$H+}{$endif}

uses
  csvutils505,
  strwrap1;

const
  LF = #10;  // line feed for multi line cells

procedure Example1;
var 
  newstr: string = ''; // buffer to hold CSV data
begin
  // adds a cell to an existing string on row 1
  CsvAddCell('test', newstr); 
  CsvAddCell('testing 123', newstr);
  CsvAddCell('testing "and" 456', newstr);
  // row 2 starting
  CsvStartNewRow(newstr);
  CsvAddCell('tester', newstr);
  // row 3 starting
  CsvStartNewRow(newstr);
  CsvAddCell('testing12345', newstr);
  CsvAddCell('testing345', newstr);
  CsvAddCell('multi line '#10'cell is here', newstr);
  CsvAddCell('multi and another multiline '#10'cell is here', newstr);
  // ALWAYS END THE CSV - this cleans up and finalizes the CSV buffer
  CsvEnd(newstr);
  writeln(newstr);
  // save a test file 
  StrSaveFile('testcsv.csv', newstr);
  writeln;
  writeln('Done example 1, hit enter');
  writeln;
  readln;
end;

begin
  Example1;
end.
Open the file called testcsv.csv and take a look using your text editor or Excel to verify the CSV was saved as Excel compatible CSV. You can change the delimiter by using the overloaded functions that allow you to specify your own delimiter. The encloser (quote) can be specified too.






lufdoc, Powtils, fpc, freepascal, delphi, kylix, c/c++, mysql, cgi web framework docs, Z505