时间:2019-05-17 标签:c#openxmlexcelstylesheetbordererror

标签 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
}

最佳答案

边框元素被定义为序列,因此具有明确的顺序。 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 },

关于时间:2019-05-17 标签:c#openxmlexcelstylesheetbordererror,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45382342/

相关文章:

Excel图表不接受x轴上的日期时间序列

objective-c - 如何在 iOS 应用程序中创建 .doc 文件或文字处理器?

vba - 如何找到丢失的用户窗体控件

c# - 如何更快地阅读 OpenXML 格式

java - Apache POI XSLFSlide 页码

c# - 仅使用公钥验证签名 (C#)

c# - ITextSharp 从 5.3.3.0 升级到 5.5.0.0 合并文件问题 - 文档关闭时已关闭

C# 设置 Outlook 默认签名

c# - ListBox 中的 WrapPanel 具有水平和垂直滚动功能

sharepoint - 如何使用页眉/页脚/页面方向将列表从 SharePoint 导出到 Excel?