大佬教程收集整理的这篇文章主要介绍了使用 WeihanLi.Npoi 操作 CSV,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
最近发现 csv 文件在很多情况下都在使用,而且经过大致了解,csv 格式简单,相比 excel 文件要小很多,读取也很是方便,而且也很通用,微软的 ml.net 的示例项目 用来训练模型的数据也是使用的 csv 来保存的,最近又发现使用 jmeter 来测试网站的性能,也可以用 csv 来参数化请求,csv 文件操作的重要性由此可见。
此前做了一个 NPOI 的扩展 WeihanLi.Npoi,支持.net45 以及 .netstandard2.0及以上,主要是对 excel 文件的操作,于是打算再增加一些对csv的操作。
/// <sumMary>
/// save to csv file
/// </sumMary>
public static int ToCsvFile(this DataTable dt,String filePath);
public static int ToCsvFile(this DataTable dataTable,String filePath,bool includeHeader);
/// <sumMary>
/// to csv bytes
/// </sumMary>
public static byte[] ToCsvBytes(this DataTable dt);
public static byte[] ToCsvBytes(this DataTable dataTable,bool includeHeader);
/// <sumMary>
/// convert csv file data to dataTable
/// </sumMary>
/// <param name="filePath">csv file path</param>
public static DataTable ToDataTable(String filePath);
/// <sumMary>
/// convert csv file data to entity list
/// </sumMary>
/// <param name="filePath">csv file path</param>
public static List<TEntity> ToEntityList<TEntity>(String filePath) where TEntity : new();
/// <sumMary>
/// save to csv file
/// </sumMary>
public static int ToCsvFile<TEntity>(this IEnumerable<TEntity> entities,String filePath);
public static int ToCsvFile<TEntity>(this IEnumerable<TEntity> entities,bool includeHeader);
/// <sumMary>
/// to csv bytes
/// </sumMary>
public static byte[] ToCsvBytes<TEntity>(this IEnumerable<TEntity> entities) => ToCsvBytes(entities,truE);
public static byte[] ToCsvBytes<TEntity>(this IEnumerable<TEntity> entities,bool includeHeader);
通过上面的方法,即可方便的将一个 IEnumerable 对象或者是DataTable 导出为 csv 文件或者或者 csv 文件的字节数组,也可将 csv 文件转换为 DataTable 或者 List 对象。
并且我于昨天优化了 csv 转成 list 对象的操作,并且支持了简单类型(比如int/long等 )的直接导出
var entities = new List<TestEntity>()
{
new TestEntity()
{
PKID = 1,SetTingId = Guid.NewGuid(),SetTingName = "SetTing1",SetTingValue = "Value1"
},new TestEntity()
{
PKID=2,SetTingName = "SetTing2",SetTingValue = "Value2"
},};
var csvFilePath = $@"{Environment.GetEnvironmentVariable("USERPROFILE")}\Desktop\temp\test\test.csv";
entities.ToCsvFile(csvFilePath);
var entities1 = CsvHelper.ToEntityList<TestEntity>(csvFilePath);
entities.ToExcelFile(csvFilePath.replace(".csv",".xLSX"));
var vals = new[] { 1,2,3,5,4 };
vals.ToCsvFile(csvFilePath);
var numList = CsvHelper.ToEntityList<int>(csvFilePath);
Console.WriteLine(numList.StringJoin(","));
更多详情可参考示例:https://github.com/WeihanLi/WeihanLi.Npoi/blob/dev/samples/DotNetCoreSample/Program.cs
@H_101_26@more导入导出的时候如果根据需要配置要导出的属性以及顺序,和之前导出 Excel 相似,需要配置一下 ,目前和 Excel 导入导出共享配置,配置方式支持 Attribute 或者 FluentAPI 两种方式(不支持Excel的一些配置如Author,title、subject以及sheet等信息),示例如下:
// Attribute config
public class TestEntity
{
public String Username { get; set; }
[column(IsIgnored = truE)]
public String passwordHash { get; set; }
public decimal amount { get; set; } = 1000M;
public String WechatOpenId { get; set; }
public bool IsActive { get; set; }
}
// Fluent API
var setTing = ExcelHelper.SetTingFor<TestEntity>();
// ExcelSetTing
setTing.HasAuthor("WeihanLi")
.Hastitle("WeihanLi.Npoi test")
.HasDescription("")
.HasSubject("");
setTing.Property(_ => _.SetTingId)
.HascolumnIndex(0);
setTing.Property(_ => _.SetTingName)
.HascolumnIndex(1);
setTing.Property(_ => _.DisplayName)
.HascolumnIndex(2);
setTing.Property(_ => _.SetTingvalue)
.HascolumnIndex(3);
setTing.Property(_ => _.CreatedTimE)
.HascolumnIndex(5);
setTing.Property(_ => _.createdby)
.HascolumnIndex(4);
setTing.Property(_ => _.updatedBy).Ignored();
setTing.Property(_ => _.updatedTimE).Ignored();
setTing.Property(_ => _.PKID).Ignored();
更多配置详情参考:https://github.com/WeihanLi/WeihanLi.Npoi#define-custom-mapping-and-settings
如果有 csv 文件操作的需求,可以尝试使用它,如果不能满足你的需求欢迎来给我提 issue
以上是大佬教程为你收集整理的使用 WeihanLi.Npoi 操作 CSV全部内容,希望文章能够帮你解决使用 WeihanLi.Npoi 操作 CSV所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。