0

I'm trying to add some styles to some cells in a generated spreadsheet.

private static Stylesheet CreateStylesheet()
    {
        Stylesheet stylesheet = new Stylesheet();

        Fonts fonts = new Fonts();
        fonts.Append(
            new DocumentFormat.OpenXml.Spreadsheet.Font(
                new Bold(),
                new FontSize() { Val = 10 },
                new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
                new FontName() { Val = "Arial Narrow" }
            )
        );
        fonts.Append(
            new DocumentFormat.OpenXml.Spreadsheet.Font(
                new FontSize() { Val = 10 },
                new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
                new FontName() { Val = "Arial Narrow" }
            )
        );
        fonts.Append(
            new DocumentFormat.OpenXml.Spreadsheet.Font(
                new Bold(),
                new FontSize() { Val = 12 },
                new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
                new FontName() { Val = "Arial Narrow" }
            )
        );
        fonts.Append(
            new DocumentFormat.OpenXml.Spreadsheet.Font(
                new Bold(),
                new FontSize() { Val = 14 },
                new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
                new FontName() { Val = "Arial Narrow" }
            )
        );

        stylesheet.Fonts = fonts;

        Fill fill3 = new Fill();
        PatternFill patternFill3 = new PatternFill() { PatternType = PatternValues.Solid };
        ForegroundColor foregroundColor1 = new ForegroundColor() { Rgb = "FFFF0000" };
        BackgroundColor backgroundColor1 = new BackgroundColor() { Indexed = (UInt32Value)64U };
        patternFill3.Append(foregroundColor1);
        patternFill3.Append(backgroundColor1);
        fill3.Append(patternFill3);

        Fills fills = new Fills();
        fills.Append(
            new Fill(
                new PatternFill() { PatternType = PatternValues.None }
            )
        );

        fills.Append(fill3);

        stylesheet.Fills = fills;

        Borders borders = new Borders();
        borders.Append(new Border());
        borders.Append(
            new Border(
                new LeftBorder(),
                new RightBorder(),
                new TopBorder(),
                new BottomBorder()
            )
        );

        stylesheet.Borders = borders;

        NumberingFormats numberingFormats = new NumberingFormats();

        NumberingFormat currencyFormat = new NumberingFormat();
        currencyFormat.NumberFormatId = UInt32Value.FromUInt32(3453);
        currencyFormat.FormatCode = StringValue.FromString("###,0 €");
        numberingFormats.Append(currencyFormat);
        stylesheet.NumberingFormats = numberingFormats;

        CellFormats formats = new CellFormats();
        formats.Append(new CellFormat
        {
            FontId = 1U,
            FillId = 0U,
            BorderId = 1U,
            ApplyFont = true,
            ApplyAlignment = true,
            Alignment = new Alignment()
            {
                Horizontal = HorizontalAlignmentValues.Center,
                Vertical = VerticalAlignmentValues.Center
            }
        });
        formats.Append(new CellFormat
        {
            FontId = 2U,
            BorderId = 1U,
            ApplyFont = true,
            Alignment = new Alignment()
            {
                Horizontal = HorizontalAlignmentValues.Left,
                Vertical = VerticalAlignmentValues.Center
            }
        });
        formats.Append(new CellFormat
        {
            FontId = 1U,
            FillId = 0U,
            BorderId = 0U,
            NumberFormatId = currencyFormat.NumberFormatId,
            ApplyFill = true,
            ApplyFont = true,
            ApplyBorder = true,
            ApplyNumberFormat = true
        });
        formats.Append(new CellFormat
            {
                FontId = 0U,
                FillId = 0U,
                BorderId = 0U,
                ApplyFont = true
            }
        );
        formats.Append(new CellFormat
            {
            FontId = 3U,
            FillId = 0U,
            BorderId = 0U,
            ApplyFont = true
            }
        );

        stylesheet.CellFormats = formats;

        return stylesheet;
    }

I need to apply some of this styles to the cells that are inserted in the sheet. I am assuming that cell formats are the available styles. Am i doing something wrong here?

This code only creates the stylesheet.

I'm using the following assembly: DocumentFormat.OpenXml (version 2.7.1) It was obtained from the source: https://github.com/OfficeDev/Open-XML-SDK

After defining the stylesheet, i create a cell with a code similar to this:

Cell cell = InsertCellInWorksheet(workSheet, addressName, 0U);
cell.CellValue = new CellValue(value);
cell.DataType = new EnumValue<CellValues>(CellValues.Number);
cell.StyleIndex = 1U;

1 Answers1

0

Check out the question "Cell styles in OpenXML spreadsheet (SpreadsheetML)"

It has an excellent answer that can probably fix your problem.

Community
  • 1
  • 1
Daniel Brixen
  • 1,633
  • 13
  • 20