Lalitha Chevuru

Lalitha Chevuru

  • NA
  • 84
  • 4.6k

Write data from memory stream to excel file.

May 7 2023 4:29 PM

Hi Team,

I am creating CSV file using memory stream which is working fine.

Now I want to create Excel file instead of CSV file.

Below is my working code, My requirement is instead of csv file need to create excel file.

using (var memoryStream = new MemoryStream())
            {
                using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
                {
                    CreateZipEntry("result.csv", sb.ToString(), archive);
                    CreateZipEntry("generated-at-" + DateTime.UtcNow.ToString("yyyy-MM-dd-HH-mm-ss") + "-UTC.txt", "", archive);
                }

                using (var fileStream = new FileStream(@"D:\Relation-Data.zip", FileMode.Create))
                {
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    memoryStream.CopyTo(fileStream);
                }             
            }

Gone through many options, however not been successful in any option.

Please help me to write data into an Excel file. this is a very urgent task for me. 

I would like to use the Interop library, Please post a piece of code for the same.

Thanks,

Lalitha.C 


Answers (4)