2015-07-21 50 views
0

我已經使用OpenXML創建了XSLX文件。打開Excel文件的OpenXML樣式警告

文件被正確創建,但自從我應用了樣式後,當我使用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; 

什麼建議嗎?

回答

0

我很確定當有空的CellFormats()時,Excel不喜歡它。 我建議你儘量去除這兩個

new CellFormat() { }, 
new CellFormat() { }, 
相關問題