FileHelpers v3.1 - 分隔符(CSV)或固定长度数据导入/导出框架






4.84/5 (445投票s)
一个易于使用的 .NET 库,用于读取/写入具有固定长度或分隔符记录(CSV)的强类型数据。还支持从不同的数据存储(Excel、Access、SqlServer、MySql)导入/导出数据。
FileHelpers v 3.1
![]() |
引言
FileHelpers 是一个易于使用的库,用于导入/导出固定长度或分隔符的平面文件(如 CSV)。FileHelpers 还支持从不同的数据存储(Excel、Access、SqlServer)导入/导出数据。
该库包含一组用于基本类型的转换器,并且可以轻松扩展以提供自定义转换器。
主要思想非常简单
您可以通过简单地描述一个类来映射文件中的每个记录,从而强类型化您的平面文件(固定长度或分隔符),然后像处理强类型的 .NET 数组一样处理您的文件。
基本用法
直接在文件和 .NET 源代码之间
它还可以用作文件与 MS Access 或 MS SqlServer 表之间的中间件。
下载说明
安装该库的推荐方法是使用 NuGet 包
www.nuget.org/packages/FileHelpers/
通过 NuGet 控制台: Install-Package FileHelpers
或者,您可以使用 Visual Studio NuGet 浏览器搜索 FileHelpers。
快速入门指南(简单的 


步骤)
要开始使用 FileHelpers 库,您只需要在项目中添加对文件 FileHelpers.dll 的引用。您可以在分发版的 Release 目录中找到它。提示:请记住保留 FileHelpers.xml 文件以获得 Intellisense 支持。
接下来,您需要定义一个类来映射源/目标文件中的记录。在本例中,我们使用一个以 | 分隔的文件,格式如下:
10248|VINET|04071996|32.38
10249|TOMSP|05071996|11.61
10250|HANAR|08071996|65.83
10251|VICTE|08071996|41.34
...............
我们引用的类可以是这样的:
[DelimitedRecord("|")]
public class Orders
{
public int OrderID;
public string CustomerID;
[FieldConverter(ConverterKind.Date, "ddMMyyyy")]
public DateTime OrderDate;
public decimal Freight;
}
然后,您必须实例化一个 FileHelperEngine 并读取/写入文件。
var engine = new FileHelperEngine<Orders>();
/// to Read use:
var res = engine.ReadFile("input.txt");
/// to Write use:
engine.WriteFile("output.txt", res);
最后,您可以使用 res 数组来访问文件中的每个项,例如:
foreach (Orders order in res)
{
Console.WriteLine("Order Info:");
Console.WriteLine(order.CustomerID + " - " +
order.OrderDate.ToString("dd/MM/yy"));
}
处理 FixedLength 文件完全相同,但您需要使用 [FixedLengthRecord] 和 [FieldFixedLength] 属性。
更多示例
我们在网站上有大量示例,可以从各个角度学习如何使用该库。
谁需要 File Helpers 库?
几乎每个项目中都需要从/向指定格式的平面文件读写数据。编写处理这些文件的代码并不难,您可以使用 String.Split
或 String.SubString
,但如果您这样做,代码有时会变得难以阅读和更改。如果您以后想支持带引号的字符串、多行、转换类型等,这些事情会变得很复杂。
因此,有了 FileHelpers,您无需担心文件结构的更改,因为您可以在几秒钟内更改类型、转换器、修剪和长度。
您还会发现 FileHelpers 在日志解析、数据仓库和 OLAP 应用程序、系统间通信、文件格式转换(例如从固定长度到 CSV 文件)、将测试数据加载到 NUnit 测试中以及更多方面非常有用!
该库旨在提供一种简单可靠的方法来完成所有这些任务。
Roslyn 分析器
我们的 Roslyn 分析器可帮助您正确使用该库。
当前的分析器实现了:
- 建议在使用构造函数中带有 typeof() 的引擎时使用泛型版本。
- 建议使用 [FieldHidden] 而不是 [FieldIgnored] 或 [FieldNotInFile]。
- 使用 FileHelpers.Dynamic 而不是 FileHelpers.RunTime 命名空间。
- 使用 IComparable 而不是过时的 IComparableRecord 。
下一个版本将实现:
- 检查记录类是否包含有效的记录属性。
- 检查记录类是否包含任何字段。
- 检查其他过时的方法。
- 将 engine.ReadFile 转换为异步版本,并使用 foreach。
您可以加入分析器的开发,加入我们的 Gitter 聊天。
https://gitter.im/MarcosMeli/FileHelpers
记录类向导
自 1.3.5 版本以来,该库一直有一个记录类向导来生成记录类。记录类向导允许您生成记录映射类,保存定义,并根据代码片段生成代码(读取文件、使用泛型读取、异步读取等等)。
主要特点
高性能
该库不使用反射来获取或设置字段值,而是使用动态代码生成。广泛的框架支持
您可以在以下环境中*)使用该库:.NET 2.0, 3.0, 4.0, 4.5, 4.6 和 Mono!!
进度通知
这是演示应用程序进度通知的屏幕截图。
从 1.4.0 版本开始,您可以获得库每个操作进度的通知,以便向用户显示反馈或任何您想要的内容。
类图
详细图
测试
该库包含超过 650 个 NUnit 测试,以确保正确性。
这是您可以提供给默认转换器的参数列表。
ConverterKind.Date
参数 1: 一个字符串,包含引擎传递给 DateTime.Parse 函数的 DateTime 格式。
参数 2: 一个字符串,包含用于字符串转换的编码。
示例
// By default the engines use: "ddMMyyyy"
[FieldConverter(ConverterKind.Date)]
public DateTime ShippedDate;
// Parse these dates: 1-1-2006 01-1-2006 30-01-2006
[FieldConverter(ConverterKind.Date, "d-M-yyyy" )]
public DateTime ShippedDate;
// Parse these dates: 1-1-2006 01-1-2006 01-30-2006
[FieldConverter(ConverterKind.Date, "M-d-yyyy" )]
public DateTime ShippedDate;
// Parse these dates: 01/3/2006 30/02/2006
[FieldConverter(ConverterKind.Date, "d/M/yyyy" )]
public DateTime ShippedDate;
// Parse these dates: 01042006 30022006
[FieldConverter(ConverterKind.Date, "ddMMyyyy" )]
public DateTime ShippedDate;
// Parse these dates: 04/January/2006 02/July/2006
[FieldConverter(ConverterKind.Date, "dd/MMMM/yyyy", "en" )] // or "en-US"
public DateTime ShippedDate;
// Parse these dates: 04/Enero/2006 02/Julio/2006
[FieldConverter(ConverterKind.Date, "dd/MMMM/yyyy", "es" )]
public DateTime ShippedDate;
您可以查看所有支持的格式字符串,请参阅 MSDN 文档中的 DateTime.ParseExact 。
以及 此处所有支持的区域性。
ConverterKind.Double, ConverterKind.Single 和 ConverterKind.Decimal
参数 1: 一个字符串,包含用作小数分隔符的字符。有效值:"." 或 ","。 默认: "."
示例
// "." is the decimal separator by default
[FieldConverter(ConverterKind.Double)]
public double Freight;
// "." is the decimal separator. (good for autodocumented code)
[FieldConverter(ConverterKind.Double, ".")]
public double Freight;
// "," is the decimal separator.
[FieldConverter(ConverterKind.Double, ",")]
public double Freight;
// The same for the other converters:
// ConverterKind.Decimal and ConverterKind.Single
整数转换器
ConverterKind.Int16, ConverterKind.Int32, ConverterKind.Int64, ConverterKind.Byte,
ConverterKind.UInt16, ConverterKind.UInt32, ConverterKind.UInt64, 和 ConverterKind.SByte
参数 1: 一个字符串,包含用作小数分隔符的字符。有效值:"." 或 ","。 默认: "."
警告: 该库在此处需要小数分隔符,并在内部创建组分隔符(例如,如果提供 ".",则使用 ",")。
示例
// "." is the decimal separator by default
// allows you to parse: 12,125 or 1,458,385
[FieldConverter(ConverterKind.Int32)]
public Int32 Amount;
// using "," as decimal separator
// allows you to parse: 12.125 or 1.458.385
[FieldConverter(ConverterKind.Int32, ",")]
public Int32 Amount;
// The same works for the rest of the converts
ConverterKind.Boolean
参数 1: 一个字符串,表示 True 值。
参数 2: 一个字符串,表示 False 值。
默认情况下,此转换器将字符串 "True"(不区分大小写)和 "1" 视为 true。将字段转换为字符串时,返回的值为 "True" 和 "False"。
示例
// By default it reads as true the strings "True" (Case insensitive) and "1"
[FieldConverter(ConverterKind.Boolean)]
public bool Shipped;
// Takes as true the string "verdad" and as false the string "mentira" (case insensitive)
[FieldConverter(ConverterKind.Boolean, "Verdad", "MenTiRa")]
public bool Shipped;
// Takes as true the string "1" and as false the string "0"
// You need these params if you want to write, because it use "True" and "False"
[FieldConverter(ConverterKind.Boolean, "1", "0")]
public bool Shipped;
// Takes as true the string "X" and as false the string "-"
[FieldConverter(ConverterKind.Boolean, "X", "-")]
public bool Shipped;
历史
版本 3.1,2015 年 7 月
大家好!
FileHelpers 今天达到了一个重要的里程碑,我们发布了 3.1 版本!
现在完全支持 .NET 4.0、4.5 和 Mono ,并包含大量新闻和增强功能。
在因各种原因暂停一段时间后,我们现在正试图更定期地更新该库。我们重新设计了主页,采用了 GeeksLabs 的 Material Design 模板。
主要更改
重大更改
次要更改
许可证(MIT)
FileHelpers 库的版权所有 © 2005-2015 Marcos Meli,但其源代码和二进制文件**可免费用于商业和非商业用途**。
如果您喜欢该库,请为本文投票。