简体   繁体   中英

Which style is the better and efficiency way to export csv file

I have two solutions to export CSV file with .net MVC, which use the NuGet package named CsvHelper.

Those two solutions work the same result, but I curious which is the better one can efficient with CPU, Memory, disk io?

there's a helper with CsvHelper

        public static void GenerateCsv<T>(Stream stream, IEnumerable<T> data)
        {
            using (var sw = new StreamWriter(stream, Encoding.UTF8))
            using (var writer = new CsvWriter(sw))
            {
                writer.WriteRecords(data);
            }
        }

        public static byte[] WriteCsvToMemory<T>(IEnumerable<T> data)
        {
            using (var memoryStream = new MemoryStream())
            using (var streamWriter = new StreamWriter(memoryStream))
            using (var csvWriter = new CsvWriter(streamWriter))
            {
                csvWriter.WriteRecords(data);
                streamWriter.Flush();
                return memoryStream.ToArray();
            }
        }

first solution using Response way

        public void ExportDemoCsv()
        {
            var data = new List<Test>();

            var fileName = "exportExample.csv";

            Response.AppendHeader("content-disposition", "attachment;filename=" + fileName);
            Response.ContentType = "text/csv";
            Response.ContentEncoding = Encoding.UTF8;

            Core.Helpers.CsvHelper.GenerateCsv(Response.OutputStream, data);
        }

the second solution using FileContentResult and get the byte[] from helper

        public ActionResult ExportDemoCsv()
        {
            var data = new List<Test>();

            var csvByte = Core.Helpers.CsvHelper.WriteCsvToMemory(data); 
            return File(csvByte, "text/csv", "exportExample.csv");
        }

It's just a question to find a way to get how to think this kind of question and how to prove two solutions

use async would be a good idea, but how can I know the benefit.

Update: I write the async mode


        public static async Task<byte[]> WriteCsvToMemory<T>(IEnumerable<T> data)
        {
            using (var memoryStream = new MemoryStream())
            using (var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8))
            using (var csvWriter = new CsvWriter(streamWriter))
            {
                foreach (var record in data)
                {
                    csvWriter.WriteRecord(record);
                    await csvWriter.NextRecordAsync();
                }
                await streamWriter.FlushAsync();
                return memoryStream.ToArray();
            }
        }

In reference to @Christian Sauer's comment, here is an async version.

public static async Task GenerateCsv<T>(Stream stream, IEnumerable<T> data)
{
    using (var sw = new StreamWriter(stream, Encoding.UTF8))
    using (var writer = new CsvWriter(sw))
    {
        foreach (var record in data)
        {
            writer.WriteRecord(record);
            await writer.NextRecordAsync();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM