【问题标题】:OpenXML SDK having borders for cell具有单元格边框的 OpenXML SDK
【发布时间】:2013-04-03 15:34:31
【问题描述】:

我有以下代码在 OpenXML SDK 中为该单元格添加值和数据类型:

Cell cell = InsertCellInWorksheet(column, row, worksheetPart);              
cell.CellValue = new CellValue(index.ToString());
cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);

对于这个单元格,如何在每一侧添加边框?我也喜欢在单元格上添加背景颜色。

我有以下但不知道如何为单元格添加边框:

Borders borders1 = new Borders() { Count = (UInt32Value)1U };

Border border1 = new Border();
LeftBorder leftBorder1 = new LeftBorder();
RightBorder rightBorder1 = new RightBorder();
TopBorder topBorder1 = new TopBorder();
BottomBorder bottomBorder1 = new BottomBorder();

border1.Append(leftBorder1);
border1.Append(rightBorder1);
border1.Append(topBorder1);
border1.Append(bottomBorder1);

borders1.Append(border1);

提前致谢

【问题讨论】:

    标签: c# openxml-sdk


    【解决方案1】:

    我建议安装Open XML 2.0 productivity tool。然后创建一个包含所需边框和颜色的空白 Excel 文档。在生产力工具中打开该文件,然后单击反映代码。然后它将为您提供生成该边框和背景颜色所需的 C# 代码。发布的代码有点长,但是如果您按照这些步骤进行操作,您应该可以使用它。

    **编辑**

    边框和填充属性存储在称为WookbookStylesPart 的单独部分中。在此部分,您将插入要应用于工作簿中单元格的边框、填充、字体等类型。这些属性存储在数组类型结构中,您可以在其中访问通过索引插入的样式。由于您可以将多种样式应用于一个单元格,因此 CellFormat 对象是存储各种样式的所有索引的地方。一旦你有一个单元格的CellFormat,它的索引需要通过StlyeIndex 属性在实际单元格上引用。这就是单元格如何知道如何在自身上应用各种样式的方式。

    这是创建边框的代码:

    public Border GenerateBorder()
    { 
        Border border2 = new Border();
    
        LeftBorder leftBorder2 = new LeftBorder(){ Style = BorderStyleValues.Thin };
        Color color1 = new Color(){ Indexed = (UInt32Value)64U };
    
        leftBorder2.Append(color1);
    
        RightBorder rightBorder2 = new RightBorder(){ Style = BorderStyleValues.Thin };
        Color color2 = new Color(){ Indexed = (UInt32Value)64U };
    
        rightBorder2.Append(color2);
    
        TopBorder topBorder2 = new TopBorder(){ Style = BorderStyleValues.Thin };
        Color color3 = new Color(){ Indexed = (UInt32Value)64U };
    
        topBorder2.Append(color3);
    
        BottomBorder bottomBorder2 = new BottomBorder(){ Style = BorderStyleValues.Thin };
        Color color4 = new Color(){ Indexed = (UInt32Value)64U };
    
        bottomBorder2.Append(color4);
        DiagonalBorder diagonalBorder2 = new DiagonalBorder();
    
        border2.Append(leftBorder2);
        border2.Append(rightBorder2);
        border2.Append(topBorder2);
        border2.Append(bottomBorder2);
        border2.Append(diagonalBorder2);
    
        return borders2;
    }
    

    这是添加填充的代码:

    public Fill GenerateFill()
    {
        Fill fill = new Fill();
    
        PatternFill patternFill = new PatternFill(){ PatternType = PatternValues.Solid };
        ForegroundColor foregroundColor1 = new ForegroundColor(){ Rgb = "FFFFFF00" };
        BackgroundColor backgroundColor1 = new BackgroundColor(){ Indexed = (UInt32Value)64U };
    
        patternFill.Append(foregroundColor1);
        patternFill.Append(backgroundColor1);
    
        fill.Append(patternFill);
    
        return fill;
    }
    

    您将需要此代码来插入边框并填充到样式部分:

    public uint InsertBorder(WorkbookPart workbookPart, Border border)
    {
        Borders borders = workbookPart.WorkbookStylesPart.Stylesheet.Elements<Borders>().First();
        borders.Append(border);
        return (uint)borders.Count++;
    }
    
    public uint InsertFill(WorkbookPart workbookPart, Fill fill)
    {
        Fills fills = workbookPart.WorkbookStylesPart.Stylesheet.Elements<Fills>().First();
        fills.Append(fill);
        return (uint)fills.Count++;
    }
    

    您需要首先获取要在 cellAddress 所在位置添加填充和边框的单元格的引用,格式为“B2”:

    public Cell GetCell(WorksheetPart workSheetPart, string cellAddress)
    {
        return workSheetPart.Worksheet.Descendants<Cell>()
                                    .SingleOrDefault(c => cellAddress.Equals(c.CellReference));
    }
    

    然后,一旦您获得了您的单元格,您需要获得属于该单元格的 CellFormat,并添加一个新的 CellFormat

    public CellFormat GetCellFormat(WorkbookPart workbookPart, uint styleIndex)
    {
        return workbookPart.WorkbookStylesPart.Stylesheet.Elements<CellFormats>().First().Elements<CellFormat>().ElementAt((int)styleIndex);
    }
    
    public uint InsertCellFormat(WorkbookPart workbookPart, CellFormat cellFormat)
    {
        CellFormats cellFormats = workbookPart.WorkbookStylesPart.Stylesheet.Elements<CellFormats>().First();
        cellFormats.Append(cellFormat);
        return (uint)cellFormats.Count++;
    }
    

    拥有CellFormat 后,您现在可以更改填充和边框属性。一旦更改了这些,您需要插入新的CellFormat,然后将该CellFormat 的索引指向单元格上的StyleIndex。这就是单元格如何知道要应用到自己的样式的方式。

     public void SetBorderAndFill(WorkbookPart workbookPart, WorksheetPart workSheetPart)
     {
          Cell cell = GetCell(workSheetPart, "B2");
    
          CellFormat cellFormat = cell.StyleIndex != null ? GetCellFormat(workbookPart, cell.StyleIndex).CloneNode(true) as CellFormat : new CellFormat();
          cellFormat.FillId = InsertFill(workbookPart, GenerateFill());
          cellFormat.BorderId = InsertBorder(workbookPart, GenerateBorder());    
    
          cell.StyleIndex = InsertCellFormat(workbookPart, cellFormat);
     }
    

    【讨论】:

    • 感谢您的回复 我确实查看了生产力工具,发现它生成的代码与我在网上看到的有很大不同。例如,我使用msdn.microsoft.com/en-us/library/office/cc861607.aspx 在现有单元格上插入单元格文本。我只是不确定如何更改背景颜色等。我不确定如何合并样式表。当我使用生产力工具进行操作时,我没有看到任何样式表。非常感谢您的帮助。
    • 命名工具是免费的,来自受信任的来源 (Microsoft),不可或缺且易于使用。是的,生成的代码“有点长”,但谁在乎呢。只需在您的项目中复制整个类generated code 并在需要的地方进行修改。我建议将代码分成多个#region 块以隐藏对您来说很无聊的东西,以便能够专注于您需要更改或复制的内容。归档未更改的生成代码。如果您稍后对模板电子表格进行小幅更改,请查看使用 diff 工具更改的代码
    【解决方案2】:

    SpreadsheetDocument 被构造为 WorkbookParts 的集合。其中之一,WorkbookStylesPart,包含文档中使用的所有样式。 WorkbookPart 包含您的Worksheets。要将样式应用于单元格或单元格区域,您需要将 StyleIndex 属性设置为 WorkbookStylesPart 中的相应样式。

    这个答案应该可以帮助您入门: https://stackoverflow.com/a/11118442/741326

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      相关资源
      最近更新 更多