Articles → .NET → Get Worksheet Name Of Excel In Asp.Net

Get Worksheet Name Of Excel In Asp.Net

Software Requirement

Prerequisite Knowledge

  1. How to create web project using visual studio?
  2. What are web forms?
  3. What are web controls?
  4. What are events in web application?
  5. What are namespaces in .net?
  6. What are classes in .net?
  7. How to add reference in the project?

Classes Used For Accessing Excel Application

  1. Application – This class represents an excel application.
  2. WorkBook – This class represents the single instance of work book within excel application
  3. Sheets – This class represents the collection of sheets in a single work book.
  4. WorkSheet – This class represents a single sheet

Steps Of Execution

  1. Create a new project
  2. Add controls on the page
  3. Adding required dlls and namespace for the code
  4. Code to get workbook name
  5. Code explanation

Create A New Project

Picture showing the project structure in the solution explorer
Click to Enlarge

Add Controls On The Page

  1. File upload control – For selecting the excel file.
  2. Button – for fetching the worksheet name from the selected excel file.

Picture showing the web page with 2 controls
Click to Enlarge

Adding Required Dlls And Namespace For The Code

using System;
using System.Runtime.InteropServices;

Code To Get Workbook Name

private string GetWorkbookName(string path) {
  string workBookName = string.Empty;

  Microsoft.Office.Interop.Excel.Application excelObject = new Microsoft.Office.Interop.Excel.Application();

  Microsoft.Office.Interop.Excel.Workbook workBookObject = null;
  Microsoft.Office.Interop.Excel.Sheets sheetObject = null;
  Microsoft.Office.Interop.Excel.Worksheet workSheetObject = null;

  //  Get information for the workbook
  workBookObject = excelObject.Workbooks.Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

  sheetObject = workBookObject.Worksheets;

  workSheetObject = (Microsoft.Office.Interop.Excel.Worksheet) sheetObject.get_Item(1); //Get the reference of second worksheet

  workBookName = workSheetObject.Name; //Get the name of worksheet.

  //  Dispose objects
  workBookObject.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);

  excelObject = null;
  workBookObject = null;
  sheetObject = null;
  workSheetObject = null;

  //  Return values
  return workBookName;

  1. In the first line of code we are declaring a variable workBookName and initializing it.
  2. In the second line of code we are creating an object of Application object. As mentioned in the previous section Application class represents excel application.
  3. In the line 3, 4 and 5 we are creating an object of WorkBook, Sheets and Worksheet class.
  4. In the sixth line we are opening the excel using the open() method. You can see from the code that in open method we are passing the path of the excel file. The open() method returns an object of type WorkBook.
  5. In the seventh line of code we are assigning all the sheets in the workbook to a variable.
  6. In the eighth line we are assigning the first sheet to a variable workSheetObject by using get_item() method.
  7. In the ninth line we are getting the workbook name using the property ‘Name’.
  8. In next lines we are disposing the objects and finally we are returning the value of workbook name using return.


Picture showing the sheet name of the selected sheet in
Click to Enlarge

Posted By  -  Karan Gupta
Posted On  -  Monday, January 7, 2013


Your Email Id  
Query/FeedbackCharacters remaining 250