C# 具有单元格边框的OpenXMLSDK
我有以下代码,用于在OpenXML SDK中添加具有该单元格的值和数据类型的单元格:C# 具有单元格边框的OpenXMLSDK,c#,openxml-sdk,C#,Openxml Sdk,我有以下代码,用于在OpenXML SDK中添加具有该单元格的值和数据类型的单元格: Cell cell = InsertCellInWorksheet(column, row, worksheetPart); cell.CellValue = new CellValue(index.ToString()); cell.DataType = new EnumValue<CellValues>(CellValues.SharedString); 提前感谢我
Cell cell = InsertCellInWorksheet(column, row, worksheetPart);
cell.CellValue = new CellValue(index.ToString());
cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);
提前感谢我建议安装。然后创建一个包含所需边框和颜色的空白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++;
}
拥有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);
}
电子表格文档的结构是工作簿部件的集合。其中之一是WorkbookStylesPart,它包含文档中使用的所有样式。工作簿部件包含您的工作表。要将样式应用于单元格或单元格范围,需要将StyleIndex属性设置为WorkbookStylesPart中的相应样式 这个答案应该有助于您开始:
感谢您的回复我确实查看了生产力工具,发现它生成的代码与我在网上看到的代码有很大不同。例如,我使用在现有单元格上插入单元格文本。我只是不知道如何改变背景颜色,等等。我不知道如何合并样式表。当我使用生产力工具进行测试时,我没有看到任何样式表。非常感谢您的帮助。此命名工具是免费的,来源可靠(Microsoft),不可或缺,易于使用。是的,生成的代码“有点长”,但谁在乎呢。只需在项目中复制整个类
生成的代码
,并根据需要进行修改。我建议将代码划分为若干#区域
块,以隐藏对您来说很无聊的内容,从而能够专注于您需要更改或复制的内容。还要归档未更改的生成代码。如果您以后对模板电子表格做了一个小的更改,请查看使用diff
工具更改的代码
public Cell GetCell(WorksheetPart workSheetPart, string cellAddress)
{
return workSheetPart.Worksheet.Descendants<Cell>()
.SingleOrDefault(c => cellAddress.Equals(c.CellReference));
}
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++;
}
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);
}