We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Download Excel from Ajax call in ASP.NET

Platform: ASP.NET Web Forms |
Control: XlsIO |
Published Date: February 14, 2019 |
Last Revised Date: April 11, 2019

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Using this library, you can create and download an Excel from AJAX call in ASP.NET Web Forms.

Steps to create and download Excel from AJAX call programmatically:

Step 1: Create a new ASP.NET Web application project.

Create ASP.NET Web Forms application in Visual Studio

Create a new ASP.NET web application

Step 2: Install the Syncfusion.XlsIO.AspNet NuGet package as reference to your .NET Framework application from NuGet.org.

Add XlsIO reference to the project

Install NuGet package

Step 3: Add a new button in the Default.aspx and add AJAX call as shown below.


<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
 <input type="button" onclick="exported()" value="Export"/>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
    function exported(e) {
            type: "POST",
            url: '/api/AjaxAPI/ExportExcel', //call your controller and action
            contentType: "application/json; charset=utf-8",
            dataType: "json",
        }).done(function () {
                //use window.location.href for redirect to download action for download the file
                window.location.href = '/api/AjaxAPI/Download';


Step 4: Include the following namespace in newly added AjaxAPI.cs file.


using Syncfusion.XlsIO;



Imports Syncfusion.XlsIO


Step 5: Include the below method ExportExcel() and Download() in AjaxAPI.cs, to create an Excel file and download it through AJAX call.


// POST api/<controller>
public void ExportExcel()
   //save the file to server temp folder
   string fullPath = Path.Combine(HostingEnvironment.MapPath("~") + FileName);
   using (ExcelEngine excelEngine = new ExcelEngine())
      IApplication application = excelEngine.Excel;
      //Create a workbook with a worksheet
      IWorkbook workbook = application.Workbooks.Create(1);
      //Insert sample text into cell “A1”
      workbook.Worksheets[0]["A1"].Text = "Hello World";
      workbook.Version = ExcelVersion.Excel2016;
      //Save the workbook 
public HttpResponseMessage Download()
   //get the temp folder and file path in server
   string fullPath = Path.Combine(HostingEnvironment.MapPath("~") + FileName);
   byte[] fileByteArray = System.IO.File.ReadAllBytes(fullPath);
   var stream = new MemoryStream(fileByteArray);
   var content = new HttpResponseMessage(HttpStatusCode.OK)
      Content = new StreamContent(stream)
   content.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");            
   return content;



Public Sub ExportExcel()
'save the file to server temp folder
   Dim fullPath As String = Path.Combine(HostingEnvironment.MapPath("~") & fileName)
   Using excelEngine As ExcelEngine = New ExcelEngine()
      Dim application As IApplication = excelEngine.Excel
      'Create a workbook with a worksheet
      Dim workbook As IWorkbook = application.Workbooks.Create(1)
      'Insert sample text into cell “A1”
      workbook.Worksheets(0)("A1").Text = "Hello World"
      workbook.Version = ExcelVersion.Excel2016
      'Save the workbook
   End Using 
End Sub
Public Function Download() As HttpResponseMessage
   Dim fullPath As String = Path.Combine(HostingEnvironment.MapPath("~") & FileName)
   Dim fileByteArray As Byte() = System.IO.File.ReadAllBytes(fullPath)
   Dim stream = New MemoryStream(fileByteArray)
   Dim content = New HttpResponseMessage(HttpStatusCode.OK) With {
            .Content = New StreamContent(stream)
   content.Content.Headers.ContentType = New MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
   Return content
End Function


A complete working example of how to create and download Excel from AJAX call in ASP.NET can be downloaded from Create Excel file.zip.

By executing the program, you will get the output Excel file as shown below.

Create Excel File

Output Excel document

Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through Formulas, adding Charts in worksheet or workbook, organizing and analyzing data through Tables and Pivot Tables, appending multiple records to worksheet using Template Markers, and most importantly PDF and Image conversions with code examples.

Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features.

An online sample link to generate Excel file.

See Also:

How to download Excel from AJAX call in ASP.NET MVC?

How to create an Excel file 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 download the uploaded file in browser?

How to generate and display Excel files in thumbnail view?


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 Web Forms UI toolkit to boost your development speed.
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile