Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Rename Excel sheets after exporting to a new Excel spreadsheet

Rename Excel sheets after exporting to a new Excel spreadsheet

Scheduled Pinned Locked Moved C#
comhelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Walaza
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • W Walaza

      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

      M Offline
      M Offline
      moon_stick
      wrote on last edited by
      #2

      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 Dave

      It definitely isn't definatley

      W 1 Reply Last reply
      0
      • M moon_stick

        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 Dave

        It definitely isn't definatley

        W Offline
        W Offline
        Walaza
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups