Rename Excel sheets after exporting to a new Excel spreadsheet
-
Hi, I have an application that takes data from a datagrid to an Excel spreadsheet. I would like to rename the sheets (after exporting) from the default Sheet1, Sheet2, etc to my own specified name. The code that I have below gives me an error which says that: 'Property or indexer 'Microsoft.Office.Interop.Excel.Sheets.this[object]' cannot be assigned to -- it is read only'. Is there any other way I can do this? //create new worksheet Microsoft.Office.Interop.Excel._Application _appClass = new ApplicationClass(); _appClass.Visible = true; _appClass.Caption = "Exported ECS Errors"; Microsoft.Office.Interop.Excel.Workbook wbk = _appClass.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); wbk.Worksheets["Sheet1"] = "Mvelo";
Mvelo Walaza Developer Telkom SA
-
Hi, I have an application that takes data from a datagrid to an Excel spreadsheet. I would like to rename the sheets (after exporting) from the default Sheet1, Sheet2, etc to my own specified name. The code that I have below gives me an error which says that: 'Property or indexer 'Microsoft.Office.Interop.Excel.Sheets.this[object]' cannot be assigned to -- it is read only'. Is there any other way I can do this? //create new worksheet Microsoft.Office.Interop.Excel._Application _appClass = new ApplicationClass(); _appClass.Visible = true; _appClass.Caption = "Exported ECS Errors"; Microsoft.Office.Interop.Excel.Workbook wbk = _appClass.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); wbk.Worksheets["Sheet1"] = "Mvelo";
Mvelo Walaza Developer Telkom SA
It looks like you're trying to assign a string value to the sheet object, rather than the property which contains the name. I've not done any interop in a while so I can't remember the syntax - something like:
wbk.Worksheets["Sheet1"].Name = "Mvelo";
should work about right. HTH DaveIt definitely isn't definatley
-
It looks like you're trying to assign a string value to the sheet object, rather than the property which contains the name. I've not done any interop in a while so I can't remember the syntax - something like:
wbk.Worksheets["Sheet1"].Name = "Mvelo";
should work about right. HTH DaveIt definitely isn't definatley
This is the error I get when I put the '.Name' code: wbk.WorkSheets["Sheet1"].Name = "Mvelo"; 'Only assignment, call, increment, decrement, and new object expressions can be used as a statement.'
Mvelo Walaza Developer Telkom SA
modified on Tuesday, April 22, 2008 2:15 AM