Creating Excel Files with Header Names Using C# Coding in ASP.NET

Introduction:

In the realm of dynamic web development, the ability to generate Excel files programmatically with header names is a valuable skill. This article serves as a comprehensive guide on how to achieve this using C# coding in ASP.NET. If you've ever wondered how to enhance your data presentation by including headers in Excel files, you're in the right place.

Preparation:

Before diving into the coding process, it's essential to be aware of related articles that cover various aspects of Excel manipulation in ASP.NET. These articles include:

  1. Upload excel file data into gridview through C# coding .
  2. Create Excel Sheet With Header Column through Coding
  3. Export Gridview data into excel sheet file through C# coding
Using C# to Create an Excel File with Header in Dotnet

Step-by-Step Guide:

Step 1: Set Up the Project in Visual Studio

Begin by creating a new ASP.NET Empty Web Site in Visual Studio. Set the project name to "CreateExcelFilewithHeader" and proceed with the necessary configurations.

Step 2: Add Reference of Excel

To interact with Excel programmatically, you need to add references. Follow these steps:

  1. Right-click on your project in Solution Explorer and select "Add Reference."
  2. Choose "Microsoft.Office.Interop.Word" and hold down CTRL while selecting "Microsoft.Office.Interop.Excel."
  3. Click OK to add the references.
using Microsoft.Office.Interop.Excel

HTML Markup: Design Page

The HTML markup is designed to include a TextBox for the desired Excel file name and a button to trigger the creation of the Excel file.


    <div style="margin:20px 50px 20px 50px;">
        <table style="border-collapse:collapse;">
         <tr>
          <td style="padding:5px 10px 5px 10px; border:1px solid #dddddd;"><asp:TextBox ID="txtcreateexcel" runat="server"></asp:TextBox></td>
          <td style="padding:5px 10px 5px 10px; border:1px solid #dddddd;">
              <asp:Button ID="BtnCreateExcel" Text="Create Excel" runat="server" 
                  onclick="BtnCreateExcel_Click" /></td>
         </tr>
        </table>
      </div>

C# Coding: Namespace and Button Click Event

Now, let's move on to the C# coding section. Include the necessary namespaces and implement the BtnCreateExcel_Click event to create an Excel file programmatically.


// C# Coding: Namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using Microsoft.Office.Interop.Excel;

// C# Coding: Button Click Event
public partial class _Default : System.Web.UI.Page
{
    protected void BtnCreateExcel_Click(object sender, EventArgs e)
    {
                // Button Click Event Logic
    }
}

C# Coding: Button Click Event Logic

The heart of the article lies in the logic of creating an Excel file with header names when the "Create Excel Sheet" button is clicked. The code utilizes the Microsoft.Office.Interop.Excel library for seamless Excel file generation.


    // C# Coding: Button Click Event
    protected void BtnCreateExcel_Click(object sender, EventArgs e)
    {    
    	// File Name and Path
        string filename = @"F:\ExcelLibrary\" + txtcreateexcel.Text + ".xls";
            
        // Check if File Already Exists
        if (File.Exists(filename))
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('File Name Alread Exist...');", true);
        }
        
        // Excel Interop Objects
        Microsoft.Office.Interop.Excel._Application oApp;
        Microsoft.Office.Interop.Excel._Worksheet oSheet;
        Microsoft.Office.Interop.Excel._Workbook oBook;
        
        // Create Excel Application
        oApp = new Microsoft.Office.Interop.Excel.Application();
        oBook = oApp.Workbooks.Add();
        oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oBook.Worksheets.get_Item(1);
        
        // Add Header Names
        oSheet.Cells[1, 1] = "Name";
        oSheet.Cells[1, 2] = "Date Of Birth";
        oSheet.Cells[1, 3] = "Gender";
        oSheet.Cells[1, 4] = "Email Address";
        oSheet.Cells[1, 5] = "Phone Number";
        Microsoft.Office.Interop.Excel.Range oRange = oSheet.Range["A1", "B3"];
        if (oApp.Application.Sheets.Count < 1)
        {
            oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oBook.Worksheets.Add();
        }
        else
        {
            oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oApp.Worksheets[1];
        }
        
        // Save and Quit Excel Application
        oBook.SaveAs(filename);
        oBook.Close();
        oApp.Quit();
    }

Download Project Source Code


Conclusion:

In conclusion, this article has provided a step-by-step guide on creating Excel files with header names using C# coding in ASP.NET. By following this tutorial, you can enhance your data representation capabilities and offer users a more structured and visually appealing experience. Feel free to download the project source code to explore and implement these functionalities in your own projects. Happy coding!

Post a Comment

0 Comments