How can I read and write CSV in a way similar to NET FileHelpers?

后端 未结 7 614
感动是毒
感动是毒 2021-02-03 12:11

Anyone know how I can import/export csv, txt files in a way similar to NET FileHelpers, but using Delphi, taking spaces and quotes into account and handling traditional CSV esca

7条回答
  •  北荒
    北荒 (楼主)
    2021-02-03 12:37

    I wrote a Dataset (TTable-like object) for Jedi project called TJvCsvDataSet that follows all CSV parsing rules in a way similar to the CSV parsing rules used by Excel and various database and report tools that import and export CSVs.

    You can install JVCL, drop a TJvCsvDataSet on your form.

    It also contains a stream class that will very quickly load a file on disk, and parse it line by line, using the correct escape rules required for CSV files, even files that include carriage-return/line-feed codes encoded within a field.

    You just drop it on your form, and set the FieldDefs property like this:

    CsvFieldDef=ABC:%,DEF:#,GHI:$,....

    There are special codes for integer, floating point, iso date-time, and other fields. It even allows you to map a wide-string field to a utf8 field in a CSV file.

    There is a designtime property editor to save you from having to declare the CSV Field Defs using the syntax above, instead you can just pick visually what the column types are.

    If you don't set up a CSV Field Def, it merely maps whatever exists in the file to string-type fields.

    Jedi JVCL: http://jvcl.delphi-jedi.org/

    JvCsvDataSet Docs:

    http://help.delphi-jedi.org/unit.php?Id=3107

    http://help.delphi-jedi.org/item.php?Id=174896

    enter image description here

提交回复
热议问题