OpenXML样式警告打开Excel文件

时间:2015-07-21 16:07:27

标签: c# openxml

我使用OpenXML创建了一个XSLX文件。

文件已正确创建,但由于我已应用样式,因此当我使用MS Excel打开文件时收到一条消息:Excel询问我是否要尝试恢复部分内容。

如果我查看日志文件,我可以看到问题是针对样式的。

这是我用于样式的代码:

private Stylesheet GenerateStyleSheet()
        {
            return new Stylesheet(
                new DocumentFormat.OpenXml.Spreadsheet.Font(
                    new DocumentFormat.OpenXml.Spreadsheet.Font(                                                               // Index 0 - The default font.
                        new DocumentFormat.OpenXml.Spreadsheet.FontSize() { Val = 11 },
                        new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
                        new DocumentFormat.OpenXml.Spreadsheet.FontName() { Val = "Calibri" }),
                    new DocumentFormat.OpenXml.Spreadsheet.Font(                                                               // Index 1 - The bold font.
                        new DocumentFormat.OpenXml.Spreadsheet.Bold(),
                        new DocumentFormat.OpenXml.Spreadsheet.FontSize() { Val = 13 },
                        new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
                        new DocumentFormat.OpenXml.Spreadsheet.FontName() { Val = "Calibri" }),
                    new DocumentFormat.OpenXml.Spreadsheet.Font(                                                               // Index 2 - The Italic font.
                        new DocumentFormat.OpenXml.Spreadsheet.Italic(),
                        new DocumentFormat.OpenXml.Spreadsheet.FontSize() { Val = 11 },
                        new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
                        new DocumentFormat.OpenXml.Spreadsheet.FontName() { Val = "Calibri" }),
                    new DocumentFormat.OpenXml.Spreadsheet.Font(                                                               // Index 2 - The Times Roman font. with 16 size
                        new DocumentFormat.OpenXml.Spreadsheet.FontSize() { Val = 16 },
                        new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
                        new DocumentFormat.OpenXml.Spreadsheet.FontName() { Val = "Times New Roman" })
                ),
                new Fills(
                    new DocumentFormat.OpenXml.Spreadsheet.Fill(                                                           // Index 0 - The default fill.
                        new DocumentFormat.OpenXml.Spreadsheet.PatternFill() { PatternType = PatternValues.None }),
                    new DocumentFormat.OpenXml.Spreadsheet.Fill(                                                           // Index 1 - The default fill of gray 125 (required)
                        new DocumentFormat.OpenXml.Spreadsheet.PatternFill() { PatternType = PatternValues.Gray125 }),
                    new DocumentFormat.OpenXml.Spreadsheet.Fill(                                                           // Index 2 - The yellow fill.
                        new DocumentFormat.OpenXml.Spreadsheet.PatternFill(
                            new DocumentFormat.OpenXml.Spreadsheet.ForegroundColor() { Rgb = new HexBinaryValue() { Value = "AFEEEE" } }
                        ) { PatternType = PatternValues.Solid })
                ),
                new Borders(
                    new Border(                                                         // Index 0 - The default border.
                        new DocumentFormat.OpenXml.Spreadsheet.LeftBorder(),
                        new DocumentFormat.OpenXml.Spreadsheet.RightBorder(),
                        new DocumentFormat.OpenXml.Spreadsheet.TopBorder(),
                        new DocumentFormat.OpenXml.Spreadsheet.BottomBorder(),
                        new DiagonalBorder()),
                    new Border(                                                         // Index 1 - Applies a Left, Right, Top, Bottom border to a cell
                        new DocumentFormat.OpenXml.Spreadsheet.LeftBorder(
                            new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
                        ) { Style = BorderStyleValues.Thin },
                        new DocumentFormat.OpenXml.Spreadsheet.RightBorder(
                            new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
                        ) { Style = BorderStyleValues.Thin },
                        new DocumentFormat.OpenXml.Spreadsheet.TopBorder(
                            new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
                        ) { Style = BorderStyleValues.Thin },
                        new DocumentFormat.OpenXml.Spreadsheet.BottomBorder(
                            new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
                        ) { Style = BorderStyleValues.Thin },
                        new DiagonalBorder())
                ),
                new CellFormats(
                    new CellFormat() { },
                    new CellFormat() { },
                    new CellFormat() { FontId = 0, FillId = 0, BorderId = 0 },                          // Index 2 - The default cell style.  If a cell does not have a style index applied it will use this style combination instead
                    new CellFormat() { FontId = 1, FillId = 0, BorderId = 0, ApplyFont = true },       // Index 3 - Bold 
                    new CellFormat() { FontId = 2, FillId = 0, BorderId = 0, ApplyFont = true },       // Index 4 - Italic
                    new CellFormat(                                                                   // Index 5 - Alignment
                        new Alignment() { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center }
                    ) { FontId = 0, FillId = 0, BorderId = 0, ApplyAlignment = true },
                    new CellFormat() { FontId = 0, FillId = 0, BorderId = 1, ApplyBorder = true },      // Index 6 - Border
                    new CellFormat() { FontId = 1, FillId = 2, BorderId = 0, ApplyFont = true }       // Index 7 - Bold + Blue fill
                )
            ); // return
        }

然后我以这种方式将样式应用于单元格:

myCell.StyleIndex = 6;

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我非常确定当有空的CellFormats()时,Excel不喜欢它。 我建议你尝试删除这两个

new CellFormat() { },
new CellFormat() { },