Articles → .NET → Get worksheet name of excel in asp.net

Get worksheet name of excel in asp.net


























Software requirement





Prerequisite knowledge



















Classes used for accessing excel application













Steps of execution

















Create a new project





Click to Enlarge


Add controls on the page











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;
}



















Output



Click to Enlarge


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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250