2X faster development
The ultimate ASP.NET MVC UI toolkit to boost your development speed.
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can create Excel file in ASP.NET MVC. Steps to create Excel file in ASP.NET MVC, programmatically:Step 1: Create a new ASP.NET web application project. Create a new ASP.NET web application Step 2: Complete the New ASP.NET Web Application – CreateXlsIOSample dialog:
New ASP.NET Web Application dialog Step 3: Install the Syncfusion.XlsIO.AspNet.Mvc5 NuGet package as reference to your .NET Framework application from NuGet.org. Install NuGet package Step 4: A default controller with name HomeController.cs gets added on creation of ASP.NET MVC project. Include the following namespace in that HomeController.cs file. C# using Syncfusion.XlsIO;
VB.NET Imports Syncfusion.XlsIO
Step 5: A default action method named Index will be present in HomeController.cs. Right click on this action method and select Go To View where you will be directed to its associated view page Index.cshtml. Step 6: Add a new button in the Index.cshtml as shown below. CSHTML @{Html.BeginForm("CreateDocument", "Home", FormMethod.Get); { <div> <input type="submit" value="Create Document" style="width:150px;height:27px" /> </div> } Html.EndForm(); }
VBHTML @Html.BeginForm("CreateDocument", "Home", FormMethod.Get) <div> <input type="submit" value="Create Document" style="width:150px;height:27px" /> </div> Html.EndForm();
Step 7: Add a new action method CreateDocument in HomeController.cs and include the below code snippet to create an Excel file and download it. C# public void CreateDocument() { //Create an instance of ExcelEngine using (ExcelEngine excelEngine = new ExcelEngine()) { //Set the default application version as Excel 2016 excelEngine.Excel.DefaultVersion = ExcelVersion.Excel2016; //Create a workbook with a worksheet IWorkbook workbook = excelEngine.Excel.Workbooks.Create(1); //Access first worksheet from the workbook instance IWorksheet worksheet = workbook.Worksheets[0]; //Insert sample text into cell “A1” worksheet.Range["A1"].Text = "Hello World"; //Save the workbook to disk in xlsx format workbook.SaveAs("Sample.xlsx", ExcelSaveType.SaveAsXLS, HttpContext.ApplicationInstance.Response, ExcelDownloadType.Open); } }
VB.NET Public Sub CreateDocument() 'Create an instance of ExcelEngine Using excelEngine As ExcelEngine = New ExcelEngine 'Set the default application version as Excel 2016 excelEngine.Excel.DefaultVersion = ExcelVersion.Excel2016 'Create a workbook with a worksheet Dim workbook As IWorkbook = excelEngine.Excel.Workbooks.Create(1) 'Access first worksheet from the workbook instance Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Insert sample text into cell “A1” worksheet.Range("A1").Text = "Hello World" 'Save the workbook to disk in xlsx format workbook.SaveAs("Sample.xlsx", ExcelSaveType.SaveAsXLS, HttpContext.ApplicationInstance.Response, ExcelDownloadType.Open) End Using End Sub
A complete working example of how to create Excel file in ASP.NET MVC can be downloaded from Create Excel file in ASP.NET MVC.zip. By executing the program, you will get the output Excel file as shown below: Output Excel document You can also open the existing Excel file and add more data using XlsIO. Please refer the online sample and UG documentation links for the same. Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features. See Also:How to download Excel from Ajax call in ASP.NET MVC? How to create an Excel file in ASP.NET Core? How to create an Excel file in ASP.NET Web Forms? How to create an Excel file in Xamarin? How to create an Excel file in Windows Forms How to create an Excel file in WPF Note: Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.
|
2X faster development
The ultimate ASP.NET MVC UI toolkit to boost your development speed.
This page will automatically be redirected to the sign-in page in 10 seconds.
if I already have a file in excel on my server as I call it and add more data
Hi Roman,
Yes, you can open the existing Excel file and add more data using XlsIO.
Please refer the online sample and UG documentation link for the same.
Online Sample Link: http://mvc.syncfusion.com/demos/web/xlsio/default
UG Documentation Link: https://help.syncfusion.com/file-formats/xlsio/loading-and-saving-workbook
Regards,
Prasanth
my man, save my day.
thnx a lot sycfusion.
Hi Mustafa,
Thank you for updating us.
Regards,
Mohan Chandran.