我建议安装
Open XML 2.5生产力工具。 然后创建一个包含所需边框和颜色的空白Excel文档。 在生产力工具中打开该文件,然后单击“反射代码”。 它将为您提供生成该边框和背景颜色所需的C#代码。 该代码有点冗长,无法发布,但如果按照这些步骤进行操作,应该能够使用它。
**编辑**
边框和填充属性存储在名为
WorkbookStylesPart
的单独部分中。 这部分是您将希望应用于工作簿单元格的边框、填充、字体等类型的样式的位置。 这些属性存储在数组类型结构中,您可以通过索引访问插入的样式。 因为您可以在单元格上应用多个样式,所以
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++;
}
您需要首先获取对要添加填充和边框的单元格的引用,其中单元格地址采用"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);
}
#region
块,以隐藏对您无聊的内容,从而能够专注于您需要更改或复制的内容。同时归档未更改的生成代码。如果以后对模板电子表格进行了小的更改,请使用“diff”工具查看哪些代码发生了变化。 - Roland