C# export Excel file to PDF
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.PDF);
ms.Position = 0;
var fileName = "demo.pdf";
return File(ms, contentType, fileName);
}