From Free Pascal wiki
Revision as of 11:14, 13 January 2011 by Vincent (talk | contribs) (Change Log: formatting)

English (en) русский (ru)


CsvDocument library is a unit contaning set of classes for CSV files handling. The library was created to exchange data with OpenOffice Calc / MS Office Excel using CSV as intermediate format.

Its main characteristics are:

  • Serial access parsing of CSV files (using TCSVParser class). This is not very handy but helps to handle large files with small memory footprint.
  • Random read/write access API based on object model (using TCsvDocument). This requires more memory but allows to edit any field of CSV file at any moment.
  • CSV implementation is compatible with such of OpenOffice Calc / MS Office Excel, that means CSV files saved from Calc/Excel can be edited using CsvDocument library and vice versa.
  • Both UTF-8 encoding and windows-xxx codepages can be used with CsvDocument library. The library uses string type for all string operations and will not do any encoding conversion for you. Keep in mind though that Excel does not support CSV files in UTF-8.
  • Support for line breaks embedded into CSV fields. It was one of the reasons to reinvent the wheel. OO Calc supports this feature as well, but MS Excel does not.
  • StringGrid-like field access with TCsvDocument.Cells[ACol, ARow]. Field access is safe, that means when you try to access non-existing field you do not get "Index out of bounds" exception, but get an empty string result. To distingush between empty and non-existing fields there are methods and properties like HasRow/HasCell and RowCount/ColCount.
  • No additional limits on field length, number of rows/fields, etc. other than performance, memory usage, string type limit of 2 Gb and signed 32 bit integer value limit.
  • As of version 0.3 and above, compliance with unofficial CSV specification at http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm


Vladimir Zhirov


modified LGPL (same as the FPC RTL and the Lazarus LCL).


The library is a single Pascal source file that only depends on FPC RTL and FCL.

Change Log

Version 0.1 (2010-01-22)

  • initial release

Version 0.2 (2010-05-31)

  • fixed bug in line ending conversion
  • fixed compilation with range checking on
  • exposed QuoteCSVString function to simplify CSV generation without TCsvDocument class
  • minor code cleanup

Version 0.3 (2011-01-14)

  • CsvParser API changed: callback methods were replaced by ResetParser/ParseNextCell methods to allow using the library in pure functional programs. See example of new API usage in TCSVDocument.LoadFromStream.
  • CsvDocument API changed: TrimEmptyCells were renamed to RemoveTrailingEmptyCells
  • support for trimming leading and trailing whitespace in fields. New TrimWhitespace property added.
  • support for search in single row/column using IndexOfRow(AString, ACol) / IndexOfCol(AString, ARow)
  • performance improvements
  • code cleanup
  • added simple test suite




The latest release is available at lazarus-ccr:


The download contains the library and demo application.

Installation and usage

Include unit CsvDocument in the uses section. Set path for it in project settings if needed.


Please send bug reports and patches to the e-mail you find in the unit source code.