Ad Code

Programmatically Create Excel File with Header Names through C# Coding

Create Excel File with Header Name by using C# Coding

Hi Friends,
Today we see how to create excel file with header|column name's through C# coding.
In previous article I explained How to

  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 - 1 :

Visual Studio






Web Site


Asp.NET Empty Web Site
Project Name : CreateExcelFilewithHeader



Step - 2 : Add Reference of Excel

  1. In Solution Explorer, right-click your project's name and then click Add Reference. The Add Reference dialog box appears.
  2. On the Assemblies page, select Microsoft.Office.Interop.Word in the Component Name list, and then hold down the CTRL key and select Microsoft.Office.Interop.Excel. If you do not see the assemblies, you may need to ensure they are installed and displayed (see How to: Install Office Primary Interop Assemblies)
  3. Click OK.
using Microsoft.Office.Interop.Excel

HTML Markup : Design Page

    <div style="margin:20px 50px 20px 50px;">
        <table style="border-collapse:collapse;">
          <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>

C# Coding

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

i'm creating excel sheet when i click on Create Excel Sheet button programmatically.

public partial class _Default : System.Web.UI.Page
    protected void BtnCreateExcel_Click(object sender, EventArgs e)
        string filename = @"F:\ExcelLibrary\" + txtcreateexcel.Text + ".xls";
        if (File.Exists(filename))
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('File Name Alread Exist...');", true);
        Microsoft.Office.Interop.Excel._Application oApp;
        Microsoft.Office.Interop.Excel._Worksheet oSheet;
        Microsoft.Office.Interop.Excel._Workbook oBook;
        oApp = new Microsoft.Office.Interop.Excel.Application();
        oBook = oApp.Workbooks.Add();
        oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oBook.Worksheets.get_Item(1);
        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();
            oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oApp.Worksheets[1];

Download Project Source Code


Post a comment