http://www.codeproject.com/Articles/25133/LINQ-to-CSV-library
详细源代码在这里
https://github.com/mperdeck/LINQtoCSV
如上地址無法現在的可以移步这里.
http://pan.baidu.com/s/1kTJv2BH
原文太长,简单的提取出来就是:
1.建立实体类
using LINQtoCSV; using System; class Product { [CsvColumn(Name = "ProductName", FieldIndex = 1)] public string Name { get; set; } [CsvColumn(FieldIndex = 2, OutputFormat = "dd MMM HH:mm:ss")] public DateTime LaunchDate { get; set; } [CsvColumn(FieldIndex = 3, CanBeNull = false, OutputFormat = "C")] public decimal Price { get; set; } [CsvColumn(FieldIndex = 4)] public string Country { get; set; } [CsvColumn(FieldIndex = 5)] public string Description { get; set; } }
2.使用
CsvFileDescription inputFileDescription = new CsvFileDescription { SeparatorChar = ‘,‘, FirstLineHasColumnNames = true }; CsvContext cc = new CsvContext(); IEnumerable<Product> products = cc.Read<Product>("products.csv", inputFileDescription); var productsByName = from p in products select p;
试验了下,默认支持 "xxx"的分割符
时间: 2024-10-11 13:40:50