FileHelpers:CsvEngine.DataTableToCsv 创建无效的 CSV
我正在使用 FileHelpers 从我的应用程序导出动态创建的数据。我有一个 DataGridView,其列和行可以由用户编辑(添加、删除、操作),所以我不知道最终将哪些数据传递给 FileHelpers。
阅读使用FileHelpers动态创建CSV文件指出了我到 CsvEngine.DataTableToCsv 方法。不幸的是,此方法既不转义包含分隔符、双引号或换行符的字段。
如果我有一个预定义的类,我可以使用 FieldQuoted-Annotation 使 FileHelper 使用正确的格式,但由于情况并非如此,我不知道如何使用动态创建的数据对其进行存档。
//编辑:一些代码/当前方法:
/// <summary>
/// Converts the Data in a DGV into a DataTable.
/// </summary>
/// <param name="dataGridView"></param>
/// <returns></returns>
public static DataTable GetDataTableFromDGV(DataGridView dgv)
{
DataTable myDT = new DataTable();
// add Columns
foreach (DataGridViewColumn col in dgv.Columns)
{
myDT.Columns.Add(col.Name);
};
// add Rows
foreach (DataGridViewRow row in dgv.Rows)
{
List<Object> line = new List<object>();
foreach (DataGridViewCell cell in row.Cells)
{
line.Add(cell.Value);
}
myDT.Rows.Add(line.ToArray<Object>());
}
return myDT;
}
public void main(){
String filename = "test.csv"
DataTable dt = GetDataTableFromDGV(this.dgv);
CsvEngine.DataTableToCsv(dt, filename, ',');
}
有什么想法吗?
I'm using FileHelpers to export dynamically created data from my application. I have a DataGridView whose columns and rows can be edited by the users (add, delete, manipulated), so I have no idea what data is finally passed to FileHelpers.
Reading Dynamically create a CSV file with FileHelpers pointed me to the CsvEngine.DataTableToCsv method. Unfortunatly, this method does neither escape fields containing the delimiter nor doublequotes nor newlines.
If I had a predefined class, I could use the FieldQuoted-Annotation to make FileHelper use a proper format, but since that is not the case, I do not know how to archive this with dynamically created data.
//Edit: Some code / Current approach:
/// <summary>
/// Converts the Data in a DGV into a DataTable.
/// </summary>
/// <param name="dataGridView"></param>
/// <returns></returns>
public static DataTable GetDataTableFromDGV(DataGridView dgv)
{
DataTable myDT = new DataTable();
// add Columns
foreach (DataGridViewColumn col in dgv.Columns)
{
myDT.Columns.Add(col.Name);
};
// add Rows
foreach (DataGridViewRow row in dgv.Rows)
{
List<Object> line = new List<object>();
foreach (DataGridViewCell cell in row.Cells)
{
line.Add(cell.Value);
}
myDT.Rows.Add(line.ToArray<Object>());
}
return myDT;
}
public void main(){
String filename = "test.csv"
DataTable dt = GetDataTableFromDGV(this.dgv);
CsvEngine.DataTableToCsv(dt, filename, ',');
}
Any Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您无法使用预定义的类时,FileHelpers 就不那么强大了。
但是,由于当您从
DataGridView
转换时会访问每个值,为什么不在那里处理转义呢?FileHelpers is not so powerful when you can't use a predefined class.
However, since you are visiting every value when you convert from your
DataGridView
, why not handle your escaping there?