Writing a memory stream to a file

 using (var memoryStream = new MemoryStream())
 {
    ...

     var fileName = $"FileName.xlsx";
     string tempFilePath = Path.Combine(Path.GetTempPath() + fileName );
     using (var fs = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write))
     {
        memoryStream.WriteTo(fs);
     }

 }

There is a very handy method, Stream.CopyTo(Stream).

using (MemoryStream ms = new MemoryStream())
{
    StreamWriter writer = new StreamWriter(ms);

    writer.WriteLine("asdasdasasdfasdasd");
    writer.Flush();

    //You have to rewind the MemoryStream before copying
    ms.Seek(0, SeekOrigin.Begin);

    using (FileStream fs = new FileStream("output.txt", FileMode.OpenOrCreate))
    {
        ms.CopyTo(fs);
        fs.Flush();
    }
}

Also, you don't have to close fs since it's in a using statement and will be disposed at the end.