C# export Excel file to CSV
Last modified: July 04, 2021Step 1
Create a ASP.NET MVC project
Step 2
Install Free Spire.XLS Nuget package.
Step 3
Replace Index() of the HomeController as below.
public IActionResult Index()
{
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"c:\temp\stock.xlsx", ExcelVersion.Version2016);
for (int i = 0; i < workbook.Worksheets.Count; i++)
{
workbook.Worksheets[i].PageSetup.IsFitToPage = true;
}
var ms = new MemoryStream();
var contentType = "application/pdf";
workbook.SaveToStream(ms, Spire.Xls.FileFormat.CSV);
ms.Position = 0;
var fileName = "demo.csv";
return File(ms, contentType, fileName);
}