I have a problem with the Cell Style in OpenXml/C# code.
I cannot define a style to a cell. I tried many ways to do this but I was unsuccessful. Can someone help me?
This part of the code, to apply the Cell Style, is:
Stylesheet stylesheet = spreadsheet.WorkbookPart.WorkbookStylesPart.Stylesheet;
WorkbookPart workbookPart = spreadsheet.WorkbookPart;
Fonts fonts = stylesheet.Fonts;//new Fonts() { Count = (UInt32Value)1U };
Font font = new Font();
FontSize newFontSize = new FontSize() { Val = fontSize };
Color newFontColor = new Color() { Rgb = new HexBinaryValue(fontColor) };
FontName newFontName = new FontName() { Val = fontName };
FontScheme fontScheme = new FontScheme() { Val = FontSchemeValues.Minor };
font.Append(newFontSize);
font.Append(newFontColor);
font.Append(newFontName);
font.Append(fontScheme);
if (bold == true)
{
Bold newBold = new Bold();
font.Append(newBold);
}
if (italic == true)
{
Italic newItalic = new Italic();
font.Append(newItalic);
}
if (underline == true)
{
Underline newUnderline = new Underline();
font.Append(newUnderline);
}
fonts.Append(font);
Fills fills = stylesheet.Fills;//new Fills() { Count = (UInt32Value)1U };
Fill fill = new Fill(new PatternFill()
{
PatternType = PatternValues.Solid,
ForegroundColor = new ForegroundColor() { Rgb = new HexBinaryValue(foregroundColor) }
});
fills.Append(fill);
Borders borders = stylesheet.Borders; //new Borders() { Count = (UInt32Value)1U };
Border border = new Border();
if (borderDefault == true)
{
BottomBorder bottomBorder = new BottomBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin };
TopBorder topBorder = new TopBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin };
LeftBorder leftBorder = new LeftBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin };
RightBorder rightBorder = new RightBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin };
border.Append(leftBorder);
border.Append(rightBorder);
border.Append(topBorder);
border.Append(bottomBorder);
borders.Append(border);
}
CellStyleFormats cellStyleFormats = stylesheet.CellStyleFormats;
UInt32 fontIndex = fonts.Count;
UInt32 fillIndex = fills.Count;
UInt32 borderIndex = borders.Count;
CellFormat cellFormat = new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = fontIndex, FillId = fillIndex, BorderId = borderIndex, FormatId = (UInt32Value)0U, ApplyFont = true, ApplyFill = true, ApplyBorder = true, ApplyAlignment = alignment };
if (alignment == false)
{
Alignment alignment1 = new Alignment() { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center };
}
else
{
Alignment alignment1 = new Alignment() { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center };
}
cellStyleFormats.Append(cellFormat);
UInt32 formatIndex = cellStyleFormats.Count;
CellStyles cellStyles = stylesheet.CellStyles;//new CellStyles() { Count = (UInt32Value)0U };
CellStyle cellStyle = new CellStyle() { Name = "Normal", FormatId = formatIndex, BuiltinId = (UInt32Value)0U };
cellStyles.Append(cellStyle);
//stylesheet.Append(cellStyleFormats);
//stylesheet.Append(fills);
//stylesheet.Append(borders);
//stylesheet.Append(cellFormat);
//stylesheet.Append(cellStyles);
stylesheet.Save();
cell.StyleIndex = formatIndex;
spreadsheet.WorkbookPart.WorkbookStylesPart.Stylesheet.Save();