0

您好我正在尝试使用 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 类并根据我的Map 类简单地导出到CSV?

另外,如何将我的 Employee.Minutes 转换为“hh:mm”并创建一个不存在的名为“Duration”的新列

谢谢

4

1 回答 1

0

我认为映射是为了读取 CSV。您将需要包括“使用 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

于 2015-03-16T00:55:53.817 回答