Hi,
How to generate 5600000 lakh records in CSV using asp.net with C#
Regards,
Pratik
Hi,
How to generate 5600000 lakh records in CSV using asp.net with C#
Regards,
Pratik
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jithu ThomasRank #437 · 5.7K reputationPosted Jun 20, 2024, 4:45 PM EDT
using Microsoft.AspNetCore.Mvc; using System.IO; using System.Text; using System.Threading.Tasks; namespace CsvGenerator.Controllers { [ApiController] [Route("[controller]")] public class CsvController : ControllerBase { [HttpGet("generate")] public async Task GenerateCsv() { var response = HttpContext.Response; response.ContentType = "text/csv"; response.Headers.Add("Content-Disposition", "attachment; filename=largefile.csv"); using (var writer = new StreamWriter(response.Body, Encoding.UTF8)) { // Write the header await writer.WriteLineAsync("Column1,Column2,Column3"); // Generate and write records for (int i = 0; i < 5600000; i++) { await writer.WriteLineAsync($"{i},Value{i},AnotherValue{i}"); } } return new EmptyResult(); } } }It is possible by using a streaming approach to write the records directly to the response output stream, minimizing memory usage.
Mukesh SagarRank #640 · 3.8K reputationPosted Jun 21, 2024, 3:20 AM EDT
We can add the large number of records asynchronously using the sollowing code snippet: