Saravanan Ponnaiah

Saravanan Ponnaiah

  • NA
  • 101
  • 74.1k

Unable to assign stylesheet to an excel cell using OpenXml

Jul 19 2015 9:21 AM
I am creating excel sheet using C# OpenXml SDK 2.0. I managed to create an excel sheet with data populated in it. But, when I try to create a stylesheet to an excel, the excel sheet is created but it throws error message when opening it and not able to open. Below is the code snippet I use to create stylesheet. If anyone have a pointer to create stylesheet for excel cell, kindly help me out. I am trying to set wrap text property to an excel cell.
 
WorkbookStylesPart wbsp = workbookpart.AddNewPart<WorkbookStylesPart>();
Stylesheet stylesheet = new Stylesheet();
CellFormats cellFormats1 = new CellFormats() { Count = (UInt32Value)2U };
CellFormat cellFormat1 = new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U };
CellFormat cellFormat2 = new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U, ApplyAlignment = true };
Alignment alignment1 = new Alignment() { WrapText = true };
cellFormat2.Append(alignment1);
cellFormats1.Append(cellFormat1);
cellFormats1.Append(cellFormat2);
wbsp.Stylesheet = stylesheet;
wbsp.Stylesheet.Save();