c#openxml excel stylesheet border错误

时间:2017-07-28 21:00:10

标签: c# excel openxml

我正在为带有单元格边框的Excel工作表制作样式表,只要我的边框不超过两个就可以工作。但是,当我添加第三个边框时,excel告诉我打开它时xml有错误,但只有当第三个边框有左右边框时才会出错。当它只是一个顶部和底部边框时,没有问题。

为什么我只为边界索引2收到错误,并且只有当它有左右边框时?

我将下面的样式表方法的完整代码,但我遇到问题的具体部分在这里:

//this is the border index 2 (third border)
new DocumentFormat.OpenXml.Spreadsheet.Border(  

    //error is caused by this section
    //if I leave the right and left border out there is no error                                                       
    //also no error if this whole border is border index 0 or 1
    new DocumentFormat.OpenXml.Spreadsheet.RightBorder(
        new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
    )
    { Style = BorderStyleValues.Thick },
    new DocumentFormat.OpenXml.Spreadsheet.LeftBorder(
        new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
    )
    { Style = BorderStyleValues.Thick },
    // end section where error is caused

    //top and bottom borders work just fine
    new DocumentFormat.OpenXml.Spreadsheet.TopBorder(
        new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
    )
    { Style = BorderStyleValues.Thick },
    new DocumentFormat.OpenXml.Spreadsheet.BottomBorder(
        new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
    )
    { Style = BorderStyleValues.Thick },
    new DiagonalBorder()
)//,

以下是我生成样式表的完整代码

private Stylesheet GenerateStyleSheet()
{
    return new Stylesheet(


        new DocumentFormat.OpenXml.Spreadsheet.Fonts(
            new DocumentFormat.OpenXml.Spreadsheet.Font(                                                               // Index 0 - The default font.
                new FontSize() { Val = 11 },
                new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
                new FontName() { Val = "Calibri" }),
            new DocumentFormat.OpenXml.Spreadsheet.Font(                                                               // Index 1 - The bold font.
                new Bold(),
                new FontSize() { Val = 11 },
                new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue() { Value = "FFFFFF" } },
                new FontName() { Val = "Calibri" }),
            new DocumentFormat.OpenXml.Spreadsheet.Font(                                                               // Index 2 - The Italic font.
                new Italic(),
                new FontSize() { Val = 11 },
                new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
                new FontName() { Val = "Calibri" }),
            new DocumentFormat.OpenXml.Spreadsheet.Font(                                                               // Index 3 - The Times Roman font. with 16 size
                new FontSize() { Val = 16 },
                new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
                new 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 0 - The default fill.
                new DocumentFormat.OpenXml.Spreadsheet.PatternFill() { PatternType = PatternValues.None }),
            new DocumentFormat.OpenXml.Spreadsheet.Fill(                                                           // Index 2 - The gray fill.
                new DocumentFormat.OpenXml.Spreadsheet.PatternFill(
                    new DocumentFormat.OpenXml.Spreadsheet.ForegroundColor() { Rgb = new HexBinaryValue() { Value = "0000FF" } }
                )
                { PatternType = PatternValues.Solid })
        ),
        new DocumentFormat.OpenXml.Spreadsheet.Borders(
            new DocumentFormat.OpenXml.Spreadsheet.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 DocumentFormat.OpenXml.Spreadsheet.Border(                                                         // Index 1 - Applies a Left, Right, Top, Bottom border to a cell
                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.Thick },
                new DiagonalBorder()
            ),
            new DocumentFormat.OpenXml.Spreadsheet.Border(                                                         // Index 1 - Applies a Left, Right, Top, Bottom border to a cell
                new DocumentFormat.OpenXml.Spreadsheet.RightBorder(
                    new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
                )
                { Style = BorderStyleValues.Thick },
                new DocumentFormat.OpenXml.Spreadsheet.LeftBorder(
                    new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
                )
                { Style = BorderStyleValues.Thick },
                new DocumentFormat.OpenXml.Spreadsheet.TopBorder(
                    new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
                )
                { Style = BorderStyleValues.Thick },
                new DocumentFormat.OpenXml.Spreadsheet.BottomBorder(
                    new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
                )
                { Style = BorderStyleValues.Thick },
                new DiagonalBorder()
            )//,
             //new DocumentFormat.OpenXml.Spreadsheet.Border(                                                         // Index 1 - Applies a Left, Right, Top, Bottom border to a cell
             //    new DocumentFormat.OpenXml.Spreadsheet.TopBorder(
             //        new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
             //    )
             //    { Style = BorderStyleValues.Thick },
             //    new DocumentFormat.OpenXml.Spreadsheet.BottomBorder(
             //        new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
             //    )
             //    { Style = BorderStyleValues.Thick },
             //    new DocumentFormat.OpenXml.Spreadsheet.RightBorder(
             //        new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
             //    )
             //    { Style = BorderStyleValues.Thick },
             //    new DiagonalBorder()
             //)

        ),
        new CellFormats(
        //new CellFormat() { FontId = 1, FillId = 1, BorderId = 0 }//debug



        new CellFormat() { FontId = 0, FillId = 0, BorderId = 0 },                          // Index 0 - The default cell style.  If a cell does not have a style index applied it will use this style combination instead
        new CellFormat(
            new Alignment()
            { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center }
            )
        { FontId = 1, FillId = 2, BorderId = 0, ApplyFont = true }
        ),
        new CellFormat(
            new Alignment()
            {
                Horizontal = HorizontalAlignmentValues.Right,
                Vertical = VerticalAlignmentValues.Bottom
            }
            )
            {
                FontId = 0, FillId = 1, BorderId = 0
            },
        new CellFormat(
            new Alignment()
            {
                Horizontal = HorizontalAlignmentValues.Right,
                Vertical = VerticalAlignmentValues.Bottom
            }
        )
        {
            FontId = 1, FillId = 0, BorderId = 2
        }
    ); // return
}

1 个答案:

答案 0 :(得分:1)

边界元素被定义为一个序列,因此具有明确的顺序。 LeftBorder必须在 RightBorder之前,所以您只需要反转代码中的顺序:

new DocumentFormat.OpenXml.Spreadsheet.LeftBorder(
    new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
)
{ Style = BorderStyleValues.Thick },
new DocumentFormat.OpenXml.Spreadsheet.RightBorder(
    new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
)
{ Style = BorderStyleValues.Thick },