How to create an Excel file using the Azure platform in ASP.NET Core?
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Using this library, you can create Excel file in Azure platform.
Steps to create Excel file in Azure platform programmatically:
Prerequisites:
- Visual studio 2017 with Azure development.
- An Azure subscription or a free account.
Step 1: Create a new ASP.NET Core Web application project and add code snippet to create Excel using XlsIO
- Create a new C# ASP.NET Core Web Application.
Create a new ASP.NET Core web application
- Install Syncfusion.XlsIO.Net.Core NuGet package as a reference to your .NET Standard applications from the NuGet.org.
Install NuGet package
- A default action method named Index will be present in HomeController.cs. Right click on Index method and select Go To View where you will be directed to its associated view page Index.cshtml.
- 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(); }
- Include the following namespace in your HomeController.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
- Add a new action method CreateDocument in HomeController.cs and include the below code snippet to create an Excel file and download it.
C#
//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"; //Saving the Excel to the MemoryStream MemoryStream stream = new MemoryStream(); workbook.SaveAs(stream); //Set the position as '0' stream.Position = 0; //Download the Excel file in the browser FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/excel"); fileStreamResult.FileDownloadName = "Output.xlsx"; return fileStreamResult; }
VB.NET
'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" 'Saving the Excel to the MemoryStream Dim stream As MemoryStream = New MemoryStream() workbook.SaveAs(stream) 'Set the position as '0' stream.Position = 0 'Download the Excel file in the browser Dim fileStreamResult As FileStreamResult = New FileStreamResult(stream, "application/excel") fileStreamResult.FileDownloadName = "Output.xlsx" Return fileStreamResult End Using
Step 2: Publish the Application to Azure
- In the Solution Explorer, right-click on the project name and select Publish.
Publish via Solution Explorer
- Now select the Microsoft Azure App Service, make sure Create New option is selected and then select Publish.
Publish via Microsoft Azure App Service
- This opens a Create App Service dialog, where we can add all the necessary resources required to run the application in Azure.
Step 3: Sign in to Azure account
In the Create App Service dialog, select Add an account, and sign in to the Azure subscription. If you're already signed in, select the account containing the desired subscription from the dropdown.
Create App Service
Step 4: Create a resource group
A resource group is a logical container into which Azure resources like web apps, databases, and storage accounts are deployed and managed.
Next to Resource Group, select New.
Provide a name for the Resource Group and select OK.
Create a resource group
Step 5: Create an App Service plan
An App Service plan specifies the location, size, and features of the web server farm that hosts your app.
Next to App Service Plan, select New.
In the Configure App Service Plan dialog, update the details as per the requirements and Select.
Create and configure an App Service Plan
Step 6: Create and publish the web app
In App Name, type a unique app name or accept the automatically generated unique name. The URL of the web app is http://<app_name>.azurewebsites.net, where <app_name> is your web app name.
Select Create to start creating the Azure resources.
Create Azure resources
Now the application is being deployed to Azure.
Deploying to Azure
Once the deployment completes, it publishes the ASP.NET Core web application to Azure, and then launches the app in the default browser.
Please make use of the below sample to create simple Excel file in Azure platform.
Sample link:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/myASPApp436687160.zip
I hope you enjoyed learning about how to create an Excel file using the Azure platform in ASP.NET
Core.
You can refer to our .NET Core Excel Library's feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET Core Excel Library example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!