简体   繁体   中英

Numbering Formats in OpenXML C#

I am writing a C# class that uses OpenXML to create XLSX files. I have managed to achieve a fair bit of formatting but not having any luck applying numbering formats (in particular currency format).

I have tried to copy / tweak other peoples code examples I have managed to find but all result in the dreaded 'Invalid Content' error when you try and open the spreadsheet. Below is the code I am currently using to create my stylesheet. I am trying to add NumberingFormats (haven't even tried referencing it in a CellFormat yet but already getting the error):

    /// <summary> A function to generate a style sheet which will allow the spreadsheet to use alternate fonts, fills, borders etc </summary>
    private Stylesheet GenerateStyleSheet2()
    {
        // Create a new style sheet
        Stylesheet newStyleSheet = new Stylesheet();

        newStyleSheet.Append(new Fonts(
                new Font(                                                               // Index 0 - The default font.
                    new FontSize() { Val = 11 },
                    new Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
                    new FontName() { Val = "Calibri" }),
                new Font(                                                               // Index 1 - The bold large font.
                    new Bold(),
                    new FontSize() { Val = 12 },
                    new Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
                    new FontName() { Val = "Calibri" }),
                new Font(                                                               // Index 2 - The bold and italic font.
                    new Bold(),
                    new Italic(),
                    new FontSize() { Val = 11 },
                    new Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
                    new FontName() { Val = "Calibri" })
                    ));

        newStyleSheet.Append(new Fills(
                new Fill(                                                               // Index 0 - The default fill.
                    new PatternFill() { PatternType = PatternValues.None }),
                new Fill(                                                               // Index 1 - The default fill of gray 125 (required)
                    new PatternFill() { PatternType = PatternValues.LightGray }),
                new Fill(                                                               // Index 2 - The orange fill.
                    new PatternFill(
                        new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "FDD5B2" } }
                    ) { PatternType = PatternValues.Solid }),
                new Fill(                                                               // Index 3 - The row highlight custom colour.
                    new PatternFill(
                        new ForegroundColor() { Rgb = new HexBinaryValue() { Value = m_RowHighlightColour } }
                    ) { PatternType = PatternValues.Solid })
                    ));

        newStyleSheet.Append(new Borders(
                new Border(                                                             // Index 0 - The default border.
                    new LeftBorder(),
                    new RightBorder(),
                    new TopBorder(),
                    new BottomBorder(),
                    new DiagonalBorder()),
                new Border(                                                             // Index 1 - Applies a Left, Right, Top, Bottom border to a cell
                    new LeftBorder(
                        new Color() { Auto = true }
                    ) { Style = BorderStyleValues.Thin },
                    new RightBorder(
                        new Color() { Auto = true }
                    ) { Style = BorderStyleValues.Thin },
                    new TopBorder(
                        new Color() { Auto = true }
                    ) { Style = BorderStyleValues.Thin },
                    new BottomBorder(
                        new Color() { Auto = true }
                    ) { Style = BorderStyleValues.Thin },
                    new DiagonalBorder())
                    ));


        // Add the numbering formats here
        uint iExcelIndex = 0;
        var numberingFormats = new NumberingFormats();
        var nformat = new NumberingFormat
        {
            NumberFormatId = UInt32Value.FromUInt32(iExcelIndex),
            FormatCode = StringValue.FromString("£#,##0.00")
        };
        numberingFormats.Append(nformat);
        newStyleSheet.Append(numberingFormats);



        newStyleSheet.Append( new CellFormats(

                // Index 0 - The default style.
                new CellFormat() { FontId = 0, FillId = 0, BorderId = 0, ApplyAlignment = true },

                // Index 1 - The header style.
                new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center }) { FontId = 1, FillId = 2, BorderId = 0, ApplyFont = true, ApplyFill = true },

                // Index 2 - The boxed border style.
                new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center }) { FontId = 0, FillId = 0, BorderId = 1, ApplyBorder = true },

                // Index 3 - The default style (with left aligned).
                new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center }) { FontId = 0, FillId = 0, BorderId = 0, ApplyAlignment = true },

                // Index 4 - The footer style.
                new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center }) { FontId = 2, FillId = 0, BorderId = 0, ApplyFont = true, ApplyAlignment = true },

                // Index 5 - The custom highlight style.
                new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center }) { FontId = 0, FillId = 3, BorderId = 0, ApplyAlignment = true, ApplyFill = true }
                ));


        // Return the style sheet
        return newStyleSheet;
    }

Been stuck on this for hours now :( Any help or pointers greatly appreciated!

Try to add the elements in the order that is in the spec of msdn for StyleSheet class. For example in worksheet, cols should be added first and then sheetdata. If you change the order, excel wont open your document.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM