使用 CSV Helper 从 class 写入单个记录
Using CSV Helper to write a single record from a class
我正在使用 https://joshclose.github.io/CsvHelper/ 中的库 CSV Helper 来尝试编写一个函数来为我导出 class。这是我的例子 class:
public class Z1000
{
public string ModelNumber { get; set; }
public string SerialNumber { get; set; }
private string InputVolts { get; set; }
private string InputHz { get; set; }
private string InputPhase { get; set; }
private string InputAmps { get; set; }
private string OutputVolts { get; set; }
private string OutputHz { get; set; }
private string OutputPhase { get; set; }
private string OutputAmps { get; set; }
private string Type { get; set; }
private string WiringDiagram { get; set; }
private string SCCR { get; set; }
private string FLA { get; set; }
private bool SuitableForUseAsServiceEquipment { get; set; } = false;
private List<string> InstanceManuals { get; set; } = new List<string>();
private List<string> Options { get; set; } = new List<string>();
private List<Fuse> Fuses { get; set; } = new List<Fuse>();
}
这个class也有几个函数,我排除了它们,因为它们对问题不重要。这是我试图用来导出单个记录的函数。
public void ExportToCsv<T>(T classToExport)
{
using (StreamWriter writer = new StreamWriter(@"C:\Data\Output.csv"))
{
var csv = new CsvWriter(writer);
csv.WriteHeader(typeof(T));
csv.WriteRecord(classToExport);
}
}
代码执行得很好,但由于某种原因,导出的 csv 文件只是空的。我在这里错过了某种步骤吗?我已经尝试了几种不同的方法,但似乎无法将其锁定。我阅读了写作文档及其示例,其中 none 似乎对我有用。我是不是完全错过了什么?
编辑:
我设置了一个断点并验证了正在写入 public 成员数据。
看来这个库需要你在输出记录后刷新记录。
来自 GitHub 网站:https://joshclose.github.io/CsvHelper/writing
Ending the Row
When you are done writing the row, you need to flush the fields and
start a new row. Flushing and starting a new row are separated so that
you can flush without creating a new row. Calling NextRecord()
will
flush for you.
我正在使用 https://joshclose.github.io/CsvHelper/ 中的库 CSV Helper 来尝试编写一个函数来为我导出 class。这是我的例子 class:
public class Z1000
{
public string ModelNumber { get; set; }
public string SerialNumber { get; set; }
private string InputVolts { get; set; }
private string InputHz { get; set; }
private string InputPhase { get; set; }
private string InputAmps { get; set; }
private string OutputVolts { get; set; }
private string OutputHz { get; set; }
private string OutputPhase { get; set; }
private string OutputAmps { get; set; }
private string Type { get; set; }
private string WiringDiagram { get; set; }
private string SCCR { get; set; }
private string FLA { get; set; }
private bool SuitableForUseAsServiceEquipment { get; set; } = false;
private List<string> InstanceManuals { get; set; } = new List<string>();
private List<string> Options { get; set; } = new List<string>();
private List<Fuse> Fuses { get; set; } = new List<Fuse>();
}
这个class也有几个函数,我排除了它们,因为它们对问题不重要。这是我试图用来导出单个记录的函数。
public void ExportToCsv<T>(T classToExport)
{
using (StreamWriter writer = new StreamWriter(@"C:\Data\Output.csv"))
{
var csv = new CsvWriter(writer);
csv.WriteHeader(typeof(T));
csv.WriteRecord(classToExport);
}
}
代码执行得很好,但由于某种原因,导出的 csv 文件只是空的。我在这里错过了某种步骤吗?我已经尝试了几种不同的方法,但似乎无法将其锁定。我阅读了写作文档及其示例,其中 none 似乎对我有用。我是不是完全错过了什么?
编辑:
我设置了一个断点并验证了正在写入 public 成员数据。
看来这个库需要你在输出记录后刷新记录。
来自 GitHub 网站:https://joshclose.github.io/CsvHelper/writing
Ending the Row
When you are done writing the row, you need to flush the fields and start a new row. Flushing and starting a new row are separated so that you can flush without creating a new row. Calling
NextRecord()
will flush for you.