Articles in this section
Category / Section

How to create the Excel file in C# when a button is placed inside an update panel

2 mins read

Syncfusion Essential XlsIO is a .NET Excel library used to create, read, and edit Excel documents. Using this library, you can create an Excel document when button is placed inside the update panel in ASP.NET Web Forms.

 

Steps to create an Excel file programmatically:

 

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

 

Create ASP.NET Web Forms application in Visual Studio

 

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

 

Add XlsIO reference to the project

 

  1. Add two buttons one inside update panel and another one outside it in the Default.aspx as shown below.

 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
         <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Button ID="Button1" runat="server" Text="Button (Inside Update Panel)" UseSubmitBehavior="false" CommandName="export" OnCommand="Button1_Command" />
        </ContentTemplate>
        </asp:UpdatePanel>
        <asp:Button ID="Button2" runat="server" Text="Button (Outside Update Panel)" UseSubmitBehavior="false" CommandName="export" OnCommand="Button1_Command" />
    
    </div>
    </form>
</body>
</html>

 

  1. Include the following namespace in Default.aspx.cs file.

 

using Syncfusion.XlsIO;

 

Imports Syncfusion.XlsIO

 

  1. Add below code in page load as UpdatePanel uses asynchronous postbacks to control which parts of the page get rendered.

 

protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
            scriptManager.RegisterPostBackControl(this.Button1);
        }

 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)     
   Dim scriptManager As ScriptManager = ScriptManager.GetCurrent(Me.Page)
   scriptManager.RegisterPostBackControl(Me.Button1)
End Sub

 

  1. Include the below code snippet in the click event of the button in Default.aspx.cs, to create an Excel file and download it.

 

//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("Output.xlsx", Response, ExcelDownloadType.Open, ExcelHttpContentType.Excel2016);
}

 

'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("Output.xlsx", ExcelSaveType.SaveAsXLS, Response, ExcelDownloadType.PromptDialog)
 
End Using 
 

 

A complete working example of how to create Excel when button is placed inside update panel in ASP.NET can be downloaded from Create-Excel-file.zip

By executing the program, you will get the Excel file as follows.

 

Create Excel File

 

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 Essential XlsIO features.

 

An online sample link to generate Excel file.

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 to link to learn about generating and registering Syncfusion license key in your application to use the components without trial message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied