0

我正在编写一个使用 OpenXML 创建 XLSX 文件的 C# 类。我已经设法实现了相当多的格式设置,但没有任何运气应用编号格式(特别是货币格式)。

我试图复制/调整我设法找到的其他人的代码示例,但是当您尝试打开电子表格时,所有这些都会导致可怕的“无效内容”错误。下面是我目前用来创建样式表的代码。我正在尝试添加 NumberingFormats (甚至还没有尝试在 CellFormat 中引用它但已经收到错误):

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

已经坚持了几个小时了:(非常感谢任何帮助或指示!

4

1 回答 1

1

尝试按照样式表类的 msdn 规范中的顺序添加元素。例如在工作表中,应先添加 cols,然后再添加 sheetdata。如果您更改顺序,excel 将不会打开您的文档。

于 2013-07-11T17:55:31.263 回答