如何在 Download/Stream 功能中使用 asp.net 网络表单中的 CsvHelper 创建 csv 文件

How to create csv file in Download/Stream functionality using CsvHelper in asp.net web forms

我在 Button Click 事件中使用了以下代码。随着数据,网页 html 正在进入 csv 文件。

var data = new[]
            {
                new Project { CustomerName = "Big Corp", Title = "CRM updates", Deadline = DateTime.Today.AddDays(-2) },
                new Project { CustomerName = "Imaginary Corp", Title = "Sales system", Deadline = DateTime.Today.AddDays(1) }
            };

        Response.ClearContent();
        Response.ContentType = "application/csv";
        Response.AddHeader("content-disposition", @"attachment;filename=""export.csv""");
        
        var preamble = Encoding.UTF8.GetPreamble();

        Response.OutputStream.Write(preamble, 0, preamble.Length);



        using (var writer = new StreamWriter(Response.OutputStream))
        using (var csvWriter = new CsvWriter(writer, System.Globalization.CultureInfo.CurrentCulture))
        {
            //csvWriter.Configuration.Delimiter = Environment.NewLine;
            csvWriter.Configuration.HasHeaderRecord = true;
            csvWriter.Configuration.AutoMap<Project>();

            //csvWriter.WriteHeader<Project>();
            //csvWriter.WriteRecords(data);

            csvWriter.Configuration.Delimiter = ",";

            csvWriter.WriteField("CustomerName");
            csvWriter.WriteField("Title");
            csvWriter.WriteField("Deadline");
            csvWriter.NextRecord();

            foreach (var project in data)
            {

                csvWriter.WriteField(project.CustomerName);
                csvWriter.WriteField(project.Title);
                csvWriter.WriteField(project.Deadline);
                csvWriter.WriteField(Environment.NewLine);
                csvWriter.NextRecord();
            }
            csvWriter.Flush();
            writer.Flush();
        }

尝试添加 Response.End();

using (var writer = new StreamWriter(Response.OutputStream))
using (var csvWriter = new CsvWriter(writer, System.Globalization.CultureInfo.CurrentCulture))
{
    csvWriter.Configuration.HasHeaderRecord = true; // Only used with WriteRecords(), defaults to true.
    csvWriter.Configuration.AutoMap<Project>();  // This is used to create a ClassMap.  CsvWriter will use AutoMap if no map is passed to it.

    csvWriter.Configuration.Delimiter = ",";

    csvWriter.WriteField("CustomerName");
    csvWriter.WriteField("Title");
    csvWriter.WriteField("Deadline");
    csvWriter.NextRecord();

    foreach (var project in data)
    {

        csvWriter.WriteField(project.CustomerName);
        csvWriter.WriteField(project.Title);
        csvWriter.WriteField(project.Deadline);
        csvWriter.WriteField(Environment.NewLine); // Not necessary. NextRecord() adds a newline. This will add a newline as a field in quotes.
        csvWriter.NextRecord();
    }
    csvWriter.Flush();  // You almost never want to use this. Mostly used by the backend code.  NextRecord calls Flush() to serialize the row to the TextWriter.
    writer.Flush();  // The using statement will do this for you.
}
Response.End();

您可以让自己轻松很多,只需使用 WriteRecords()。唯一需要的配置是 csvWriter.Configuration.Delimiter = ","; if CurrentCulture 使用不同的定界符。否则您添加的所有其他配置都是默认配置。

using (var writer = new StreamWriter(Response.OutputStream))
using (var csvWriter = new CsvWriter(writer, System.Globalization.CultureInfo.CurrentCulture))
{
    csvWriter.Configuration.Delimiter = ",";
                
    csvWriter.WriteRecords(data);
}
Response.End();