.net Core Use File export workbook as xlsx, can not open,when renamed as xls can open

Hi,Now I use .net core cshtml to export a excel
I write in cshtml.cs
using ( var workbook = Generate() ) {
var stream = workbook.SaveToStream().ToArray();
return File(stream, “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”, $“Test.xlsx”);
}
But I cannot open it as xlsx ,only can open it when xlsx.
could you please tell me how can I export as xlsx.
Thanks very much .

@weiwei_si,

Thanks for providing us sample code segment and details.

Well, Workbook.SaveToStream is an older method used to save the workbook to XLS format only and not for XLSX (Excel 2007/2010/2013 etc.) format.

  So, you need to change your code a bit. 

e.g
Sample code:

............      
//Save the workbook to stream
                  MemoryStream stream = new MemoryStream();
                  workbook.Save(stream, SaveFormat.Xlsx);
                  stream.Seek(0, SeekOrigin.Begin); 
............

Let us know if you still find any issue.

Thank you very much ,It worked

@weiwei_si,

Good to know that your issue is sorted out by the suggested code. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.