电子表格的样式不起作用

时间:2017-02-15 16:28:00

标签: c# openxml openxml-sdk

我尝试在生成的电子表格中为某些单元格添加一些样式。

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;
    }

我需要将一些样式应用于插入到工作表中的单元格。我假设单元格格式是可用的样式。我在这里做错了吗?

此代码仅创建样式表。

我使用以下程序集:DocumentFormat.OpenXml(2.7.1版) 它是从来源获得的:https://github.com/OfficeDev/Open-XML-SDK

定义样式表后,我创建一个代码类似于此的单元格:

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

1 个答案:

答案 0 :(得分:0)

查看问题&#34; Cell styles in OpenXML spreadsheet (SpreadsheetML)&#34;

它有excellent answer可能可以解决您的问题。