CsvHelper Convert使用不更改输出 [英] CsvHelper ConvertUsing not changing output
问题描述
我尝试使用 CsvHelper 的 ConvertUsing
方法
我已经阅读了关于 我使用的是一个简单的类: 使用此 但是输出文件 我正在寻找的输出是: 目前 如果要自定义输出,可以使用自定义类型转换器。通过类型转换器选项,你也有一些有限的能力。 I'm trying to use the I've read the documentation about ConvertUsing but can't get it to work. I'm using a simple class: With this My code which uses these creates an instance of the class and then writes it to CSV: However the output file The output I'm looking for is: And the Any ideas where I'm going wrong? Currently You can use a custom type converter if you want to customize the output. You also have some limited abilities through the type converter options. 这篇关于CsvHelper Convert使用不更改输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
public class Test
{
public long Id {get;组; }
public string标题{get;组; }
}
ClassMap
:
public class TestClassMap:CsvClassMap< Test>
{
public override void CreateMap()
{
Map(m => m.Id).Name(id)。ConvertUsing(row => 11111) ;
Map(m => m.Title).Name(title)。ConvertUsing(row => row.GetField(title)+123);
}
}
我使用这些的代码创建了类的实例然后将其写入CSV:
var test = new Test(){Id = 99,Title =Test title} ;
using(var streamWriter = new StreamWriter(test.csv))
{
var csv = new CsvWriter(streamWriter);
csv.Configuration.RegisterClassMap< TestClassMap>();
csv.WriteRecord(test);
}
test.csv
始终采用以下格式:
id,title
99,测试标题
id,title
11111,测试标题123
ConvertUsing
正在被忽略。我试过只转换 Id
,只有标题
,但这也不工作。 ConvertUsing
仅在读取时使用。
ConvertUsing
method of the CsvHelper library (v 2.4.0).public class Test
{
public long Id { get; set; }
public string Title { get; set; }
}
ClassMap
:public class TestClassMap : CsvClassMap<Test>
{
public override void CreateMap()
{
Map(m => m.Id).Name("id").ConvertUsing(row => 11111);
Map(m => m.Title).Name("title").ConvertUsing(row => row.GetField("title") + " 123");
}
}
var test = new Test() { Id = 99, Title = "Test title" };
using (var streamWriter = new StreamWriter("test.csv"))
{
var csv = new CsvWriter(streamWriter);
csv.Configuration.RegisterClassMap<TestClassMap>();
csv.WriteRecord(test);
}
test.csv
is always the following format:id,title
99,Test title
id,title
11111,Test title 123
ConvertUsing
is being ignored. I've tried only converting the Id
, and only the Title
, but this doesn't work either.ConvertUsing
is only used when reading.