I have an Excel template with various sheets on which I dump data received from SQL Server using OpenXML, C #. After I finished dumping the data, I need to hide some sheets based on conditions. I could not find any piece of code to hide a specific sheet using C # OpenXML.
I tried the following, but the sheets were not hidden.
byte[] byteArray = File.ReadAllBytes("D:\\rptTemplate.xlsx");
using (MemoryStream mem = new MemoryStream())
{
mem.Write(byteArray, 0, (int)byteArray.Length);
using (SpreadsheetDocument rptTemplate = SpreadsheetDocument.Open(mem, true))
{
foreach (OpenXmlElement oxe in (rptTemplate.WorkbookPart.Workbook.Sheets).ChildElements)
{
if(((DocumentFormat.OpenXml.Spreadsheet.Sheet)(oxe)).Name == "ABC")
((DocumentFormat.OpenXml.Spreadsheet.Sheet)(oxe)).State = SheetStateValues.Hidden;
}
rptTemplate.WorkbookPart.Workbook.Save();
}
}
Request help on this.
Thank.
Raghu source
share