如何以类似于NET FileHelpers的方式读取和写入CSV? [英] How can I read and write CSV in a way similar to NET FileHelpers?

查看:220
本文介绍了如何以类似于NET FileHelpers的方式读取和写入CSV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道如何以类似于NET FileHelpers的方式导入/导出csv,txt文件,但使用Delphi,考虑空格和引号,并以类似于CSV转义方式处理传统的CSV转义规则Excel?

Anyone know how I can import/export csv, txt files in a way similar to NET FileHelpers, but using Delp taking spaces and quotes into account and handling traditional CSV escaping rules in a manner similar to the way CSV escaping works in Excel?

ref。链接 http://www.filehelpers.com/

如果你的答案倾向于:为什么这个懒惰的家伙不写一个简单的CSV解析器,考虑这5分钟阅读,然后你会知道为什么CSV解析不是微不足道:

If your answer tends to be: "why this lazy guy dont write a simple CSV parser", consider this 5 minutes reading and then you will know why CSV parsing is not trivial:

http://secretgeek.net/csv_trouble.asp

推荐答案

我为Jedi项目编写了一个名为TJvCsvDataSet的数据集(TTable样对象),它遵循所有CSV解析规则,类似于Excel使用的CSV解析规则以及导入和导出CSV的各种数据库和报表工具。

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.

您可以安装JVCL,在表单上放置一个TJvCsvDataSet。

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

它还包含一个流类非常快速地加载磁盘上的文件,并使用CSV文件所需的正确转义规则逐行解析,甚至包括在字段中编码的回车/换行码的文件。

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.

你只需将它放在你的表单上,并设置FieldDefs属性如下:

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

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

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

有整数,浮点,iso日期时间和其他字段的特殊代码。它甚至允许您将宽字符串字段映射到CSV文件中的utf8字段。

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.

有一个designtime属性编辑器,可以避免使用上面的语法声明CSV字段定义,而只是直观地选择列类型。

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.

如果您没有设置CSV字段定义,它只会将文件中存在的任何内容映射到字符串类型字段。

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/

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

JvCsvDataSet文件:

JvCsvDataSet Docs:

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

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

这篇关于如何以类似于NET FileHelpers的方式读取和写入CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆