Delphi TQuery 保存到 csv 文件
我想将 TQuery 的内容导出到 CSV 文件,而不使用 3d 部件组件(Delphi 7)。据我所知,这不能用 Delphi 标准组件来完成。
我的解决方案是将内容保存在 CSV 格式的 StringList 中,并将其保存到文件中。
有什么舒服的解决办法吗?
PS:我不想使用 JvCsvDataSet 或任何组件。问题是:这只能用Delphi 7或更高标准的组件来完成吗?
先感谢您!
I want to export content of a TQuery to a CSV file without using a 3d part component(Delphi 7). From my knowledge this can not be accomplished with Delphi standard components.
My solution was to save the content in a StringList with a CSV format, and save it to a file.
Is there any comfortable solution?
PS:I don't want to use JvCsvDataSet or any component. Question is: can this be accomplished only with Delphi 7 or higher standard components?
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当然可以。
您只需完成正确输出 CSV 内容的工作(正确引用、处理嵌入的引号和逗号等)。您可以使用
TFileStream
轻松编写输出,并正确使用TQuery.Fields
和TQuery.FieldCount
获取数据。我将把精美的 CSV 引用和特殊处理留给您。这将解决简单的部分:
Of course it can.
You just have to do the work to properly output the CSV content (quoting properly, handling embedded quotes and commas, etc.). You can easily write the output using
TFileStream
, and get the data using theTQuery.Fields
andTQuery.FieldCount
properly.I'll leave the fancy CSV quoting and special handling to you. This will take care of the easy part:
最初的问题要求使用 StringList 的解决方案。所以它会更像这样。它适用于任何 TDataSet,而不仅仅是 TQuery。
您可以添加选项来更改分隔符类型或其他内容。
The original question asked for a solution using a StringList. So it would be something more like this. It will work with any TDataSet, not just a TQuery.
You can add options to change the delimiter type or whatever.
这类似于 Rob McDonell 解决方案,但有一些增强功能:标头、转义字符、仅在需要时才包含以及“;”分隔符。
如果不需要,您可以轻松禁用此增强功能。
This is like the Rob McDonell solution but with some enhancements: header, escape chars, enclosure only when required, and ";" separator.
You can easily disable this enhancements if not required.
Delphi 不提供对 .csv 数据的任何内置访问。
然而,按照 VCL TXMLTransform 范例,我编写了一个 TCsvTransform 类帮助器,它将 .csv 结构转换为 TClientDataSet 或从 TClientDataSet 转换。
至于最初的问题是将 TQuery 导出到 .csv,一个简单的 TDataSetProvider 将在 TQuery 和 TClientDataSet 之间建立链接。
有关 TCsvTransform 的更多详细信息,请参见 http://didier.cabale.free.fr/delphi。 htm#uCsvTransform
Delphi does not provide any built-in access to .csv data.
However, following the VCL TXMLTransform paradigm, I wrote a TCsvTransform class helper that will translate a .csv structure to /from a TClientDataSet.
As for the initial question that was to export a TQuery to .csv, a simple TDataSetProvider will make the link between TQuery and TClientDataSet.
For more details about TCsvTransform, cf http://didier.cabale.free.fr/delphi.htm#uCsvTransform
很抱歉回答一个七年前的问题,但如果有人偶然发现它(就像我一样)并且不想实现和维护自己的 CSV 编写器,下面的代码演示了如何使用 FireDAC 组件(自 Delphi 起就安装了 FireDAC 组件) XE5)只需几个基本步骤即可将数据集保存为 CSV:
Sorry to reply to a seven year old question but if anyone stumbles upon it (as I did) and doesn't want to implement and maintain their own CSV writer, the following code demonstrates how to use FireDAC components (which come installed with Delphi since XE5) to save a dataset to CSV in just a few basic steps: