We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

xlsio worksheet (tab) name

I am trying to get the name of an existing worksheet (Sheet1, Sheet2, etc) using the following statements. However, GetSheetNameByReference is an internal protected method so the following does not work.

 IWorkbook workbook = application.Workbooks.Open(d.ResolveApplicationDataPath(@"Products_09172014.xlsx"),
                ExcelOpenType.Automatic);

var x = ((WorkbookImpl)workbook).GetSheetNameByReference(0);

Is there another technique to get the sheet name besides trying to use GetSheetNameByReference in this way ?



3 Replies

SR Sudha Ramachandran Syncfusion Team September 25, 2014 06:04 AM UTC

Hi Mike,

Thank you for using Syncfusion products.

Name of the worksheets can be accessed by accessing the worksheets or tabsheets collection as referred in the below code snippet.

Code Snippet:

 

//Access worksheet name using index of the worksheet

string sheetName = workbook.Worksheets[0].Name;

 

//Access worksheet name using tab sheet index

string worksheetName = ((IWorksheet)workbook.TabSheets[0]).Name;

 

Please let us know if you need any clarification.

 

Regards,

Sudha R



CP Catalin Pop October 5, 2016 04:25 PM UTC

worksheet.Name = "New Name";                
var newworksheet = workbook.Worksheets.AddCopy(worksheet);
workbook.TabSheets[newworksheet .TabIndex].Name = worksheet.Name;

I'm having a similar problem. I try to change the name of the worksheet but when I save the workbook and open it with Office the names are Sheet2 Sheet3 and so on.


AV Abirami Varadharajan Syncfusion Team October 6, 2016 12:39 PM UTC

Hi Catalin, 

Thank you for contacting Syncfusion support. 

We are unable to reproduce the issue. As you are trying to set same name(New Name) for copied sheet as source worksheet name, the exception is thrown indicates “Name of worksheet must be unique in a workbook.” As per MSExcel behavior. We request you to set different name for copied worksheet as provided below to avoid this exception. 

Code Example: 
            worksheet.Name = "New Name"; 
            worksheet.Range["A1"].Text = "SourceWorksheet"; 
            var newworksheet = workbook.Worksheets.AddCopy(worksheet); 
            workbook.TabSheets[newworksheet.TabIndex].Name = "CopiedWorksheet"; 

We have also shared sample for your reference which can be downloaded from following location. 
 

Regards, 
Abirami. 


Loader.
Live Chat Icon For mobile
Up arrow icon