请问有人能够解释一下OpenXml中的样式索引是如何工作的吗?我有一个业务需求需要在Excel表格中将某些单元格的背景颜色设为不同的颜色。但是其他单元格已经应用了某些样式,因此我需要决定要应用哪种样式索引。
请问有人能够解释一下OpenXml中的样式索引是如何工作的吗?我有一个业务需求需要在Excel表格中将某些单元格的背景颜色设为不同的颜色。但是其他单元格已经应用了某些样式,因此我需要决定要应用哪种样式索引。
Cell cell16 = new Cell(){ CellReference = "HU1", StyleIndex = (UInt32Value)1U, DataType = CellValues.SharedString };
<x:c r="HU1" s="1" t="s">
<x:v>0</x:v>
</x:c>
StyleIndex
是样式表部分中 CellFormat
记录的从零开始的索引。
样式表部分(也称工作簿的样式表)包含以下几个部分:
现在,在CellFormat
记录中,有引用返回到样式表中每个部分的如下引用:
代码中的示例单元格格式如下:
// this line is important to your question
CellFormat cellFormat5 = new CellFormat(){ NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)11U,
//the rest of the CellFormat definition is not so important to your question
FormatId = (UInt32Value)0U, ApplyFill = true, ApplyBorder = true, ApplyAlignment = true };
将某些单元格应用特定的背景颜色。
例如,我们想要更新您电子表格中的单元格B3
,并且B3
已经具有StyleIndex
为10
。
您需要按照以下步骤进行操作:
第一步。如果这是电子表格中的新背景颜色,请将该背景(也称为Fill
)添加到包含新颜色的样式表(上面的第三个项目)的Fills
部分。如果颜色已经存在,则需要找到并记住该颜色对应的现有Fill
的索引。无论哪种方式,对于此示例,假设您需要的Fill
索引为25
。
第二步。创建一个新的CellFormat
,它是索引为10
的CellFormat
的副本。您将把此新的CellFormat
添加到CellFormat
部分的末尾。假设新的CellFormat
的索引将为53
。
CellFormat
并将其 Fill
索引属性设置为来自步骤 1 的 25
。B3
,使其具有新的 StyleIndex
为 53
。
CellFormat
orCellFormats
? I have sample document, which is used twoCellFormats
and the first one has oneCellFormat
and the other, threeCellFormat
s inside of it. You wroteCellFormat
but then point to >Cell Formats <== cell styleindex is referring to one of these
- Hassan Faghihi