CSVHelper C# 从我的对象加载
CSVHelper C# Load from My object
您好,我正在尝试使用 CSVHelper 将对象列表导出为 CSV。
(http://joshclose.github.io/CsvHelper)
所以我有这个:
List<Employee> employeeList = GetAllEmployees();
我创建了这张地图:
public sealed class EmployeeExport: CsvClassMap<Employee>
{
public EmployeeExport()
{
Map(m => m.Id);
Map(m => m.Date).TypeConverterOption("dd/MM/yyyy");
Map(m => m.Account.AccountName);
Map(m => m.LabourChargeType.LabourChargeTypeName);
}
}
如何使用我的 employeeList 加载 ExployeeExport class 并根据我的地图 class 简单地导出为 CSV?
另外,如何将我的 Employee.Minutes 转换为 "hh:mm" 并创建一个不存在的名为 "Duration"
的新列
谢谢
我认为映射是为了读取 CSV。您需要包括 "using System.IO"
编辑:
// Populate your employeeList
TextWriter textWriter = new StreamWriter("foo.csv"); //foo.csv is the file you are saving to
var csv = new CsvWriter(textWriter);
foreach (var employee in employeeList)
{
csv.WriteField(employee.Id);
csv.WriteField(employee.Date.ToShortDateString());
csv.WriteField(employee.Account.AccountName);
csv.WriteField(employee.LabourChargeType.LabourChargeTypeName);
csv.NextRecord();
}
textWriter.Flush();
textWriter.Close();
textWriter = null;
参考: http://csharp.net-informations.com/file/csharp-textwriter.htm
您好,我正在尝试使用 CSVHelper 将对象列表导出为 CSV。 (http://joshclose.github.io/CsvHelper)
所以我有这个:
List<Employee> employeeList = GetAllEmployees();
我创建了这张地图:
public sealed class EmployeeExport: CsvClassMap<Employee>
{
public EmployeeExport()
{
Map(m => m.Id);
Map(m => m.Date).TypeConverterOption("dd/MM/yyyy");
Map(m => m.Account.AccountName);
Map(m => m.LabourChargeType.LabourChargeTypeName);
}
}
如何使用我的 employeeList 加载 ExployeeExport class 并根据我的地图 class 简单地导出为 CSV?
另外,如何将我的 Employee.Minutes 转换为 "hh:mm" 并创建一个不存在的名为 "Duration"
的新列谢谢
我认为映射是为了读取 CSV。您需要包括 "using System.IO"
编辑:
// Populate your employeeList
TextWriter textWriter = new StreamWriter("foo.csv"); //foo.csv is the file you are saving to
var csv = new CsvWriter(textWriter);
foreach (var employee in employeeList)
{
csv.WriteField(employee.Id);
csv.WriteField(employee.Date.ToShortDateString());
csv.WriteField(employee.Account.AccountName);
csv.WriteField(employee.LabourChargeType.LabourChargeTypeName);
csv.NextRecord();
}
textWriter.Flush();
textWriter.Close();
textWriter = null;
参考: http://csharp.net-informations.com/file/csharp-textwriter.htm