更新时间:2023-09-18 10:23:22
当您初看OpenXML样式时,可能会造成混淆. Excel文档样式属于SpreadsheetML
标记语言,与Word和PowerPoint不同.
OpenXML styling can be confusing when you take the first look at it. Excel document styling falls under the SpreadsheetML
markup language, which is different from Word and PowerPoint..
对于Excel中的典型单元格,所需的唯一样式信息是StyleIndex(如您所指出的).
For typical cells in Excel, the only style information required is the StyleIndex (as you pointed out).
Cell cell16 = new Cell(){ CellReference = "HU1", StyleIndex = (UInt32Value)1U, DataType = CellValues.SharedString };
这对应于XML中的s属性:
This corresponds to the s attribute in the XML:
<x:c r="HU1" s="1" t="s">
<x:v>0</x:v>
</x:c>
StyleIndex
是样式"部分中CellFormat
记录的从零开始的索引.
The StyleIndex
is the zero-based index of the CellFormat
record in the Styles part.
样式部分(即工作簿的样式表)包含以下部分:
The styles part (aka the Stylesheet of the workbook) contains the following sections:
在CellFormat
记录中,现在有一些引用可以追溯到样式表中的以下每个部分:
Now inside the CellFormat
record, there are references that refer back out to each of the following sections in the stylesheet:
代码中的单元格格式示例如下:
An example cell format in code looks like:
// 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
.
To answer your question: apply a certain background color to certain cells . Let say we want to update cell B3
of your spreadsheet and B3
already has StyleIndex
of 10
.
您将需要执行以下步骤:
You will need to follow these steps:
步骤1.如果这是电子表格的新背景色,则将背景(又名Fill
)添加到包含新颜色的样式表(上方第三个项目符号)的Fills
部分中.如果颜色已经存在,则需要查找并记住该颜色的现有Fill
的索引.无论哪种方式,对于本示例来说,假设您要求的Fill
索引都是25
.
Step 1. If this is a new background color to the spreadsheet, add the background (aka Fill
) to the Fills
section of the Stylesheet (third bullet above) that contains your new color. If the color already exists, you need to find and remember the index of the existing Fill
for that color. Either way, for this example lets say the Fill
index you requre is 25
.
第2步.创建一个新的CellFormat
,它是索引10
处的CellFormat
的副本.您将把这个新的CellFormat
添加到CellFormat
部分的末尾.假设新的CellFormat
的索引将为53
.
Step 2. Create a new CellFormat
that is a copy of the CellFormat
at index 10
. You will add this new CellFormat
to the end of the CellFormat
section. Lets say the index of the new CellFormat
will be 53
.
步骤3.在索引53
处更新CellFormat
,并将其Fill
索引属性设为25
(来自步骤1).
Step 3. You update the CellFormat
at index 53
and make its Fill
index property be 25
(from Step 1).
最后一步:更新有问题的单元格B3
,将其新的StyleIndex
设置为53
Last Step: Update the Cell in question B3
, to have a new StyleIndex
of 53
注意:此答案适用于Excel中的非表格单元格样式-如果您要获取表格单元格的样式信息,请回复,然后我会尝试为其更新或添加答案.
Note: This answer is for non-table cell styling in Excel - if you want styling information for table cells, please reply and Ill try and update or add an answer for it.
这个答案主要来自我的经验,也来自免费电子书第73-79页的解释:
This answer comes mainly out of my experience and also interpretation of pages 73-79 of the free e-book: Open XML - The markup explained - by Wouter van Vugt. It is a good reference to use for all OpenXml.