Articles → .NET → Get Worksheet Name Of Excel In Asp.Net
Get Worksheet Name Of Excel In Asp.Net
Software Requirement
Prerequisite Knowledge
- How to create web project using visual studio?
- What are web forms?
- What are web controls?
- What are events in web application?
- What are namespaces in .net?
- What are classes in .net?
- How to add reference in the project?
Classes Used For Accessing Excel Application
- Application – This class represents an excel application.
- WorkBook – This class represents the single instance of work book within excel application
- Sheets – This class represents the collection of sheets in a single work book.
- WorkSheet – This class represents a single sheet
Steps Of Execution
- Create a new project
- Add controls on the page
- Adding required dlls and namespace for the code
- Code to get workbook name
- Code explanation
Create A New Project
Click to Enlarge
Add Controls On The Page
- File upload control – For selecting the excel file.
- Button – for fetching the worksheet name from the selected excel file.
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.Quit();
Marshal.ReleaseComObject(workSheetObject);
Marshal.ReleaseComObject(workBookObject);
Marshal.ReleaseComObject(excelObject);
excelObject = null;
workBookObject = null;
sheetObject = null;
workSheetObject = null;
// Return values
return workBookName;
}
- In the first line of code we are declaring a variable workBookName and initializing it.
- 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.
- In the line 3, 4 and 5 we are creating an object of WorkBook, Sheets and Worksheet class.
- 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.
- In the seventh line of code we are assigning all the sheets in the workbook to a variable.
- In the eighth line we are assigning the first sheet to a variable workSheetObject by using get_item() method.
- In the ninth line we are getting the workbook name using the property ‘Name’.
- In next lines we are disposing the objects and finally we are returning the value of workbook name using return.
Output
Click to Enlarge