FileHelpers:CsvEngine.DataTableToCsv 创建无效的 CSV

发布于 2024-12-16 02:06:22 字数 1625 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方。的韩爷 2024-12-23 02:06:22

当您无法使用预定义的类时,FileHelpers 就不那么强大了。

但是,由于当您从 DataGridView 转换时会访问每个值,为什么不在那里处理转义呢?

foreach (DataGridViewCell cell in row.Cells)
{
    line.Add(AddDoubleQuotesAndEscapeIfNecessary(cell.Value));
}

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?

foreach (DataGridViewCell cell in row.Cells)
{
    line.Add(AddDoubleQuotesAndEscapeIfNecessary(cell.Value));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文