Search code examples
c#exceloffice-interop

Write to a hidden Excel Worksheet with c#


I am writing a Add-In for excel where i have to store workbookspecific settings in the excel file. I tryed to create a new worksheet, and hide it.

settingsSheet.Visible = XlSheetVisibility.xlSheetHidden;

Now my application was always writing to my open worksheet, not the hidden settingsworksheet. If i remove the part which hides the worksheet, it works fine.

Is there a way to write to hidden excel worksheets? Or is there a better way to store settings?

edit: aditional code

Worksheet newWorksheet;
string newWorksheetname = "SettingsSheet";
foreach (Worksheet sheet in ActiveWorkbook.Sheets)
        {
            if (sheet.Name == newWorksheetname)
            {
                newWorksheet = sheet;                   
            }
        }
if (newWorksheet == null)
 {
  newWorksheet = (Worksheet)Globals.ThisAddIn.Application.Worksheets.Add();
            newWorksheet.Name = newWorksheetname;
  }
newWorksheet.Visible = XlSheetVisibility.xlSheetHidden;
newWorksheet.Cells[1,1].Value="settingsValue";

Solution

  • I didn't found any solution to my problem so I found a method of storing xml in a excel file but not in a hidden sheet. What is the best way to store XML data in an Excel file