Spreadsheet Styles 电子表格样式
Spreadsheets can be styled using styles, themes, and direct formatting. 可以使用样式、主题和直接格式设置电子表格的样式。There are cell styles, table styles, and pivot styles. However, unlike in WordprocessingML, styling XML never appears with the content in a worksheet. 有单元样式、表格样式和轴样式。然而,和WordprocessingML不同的是,XML样式从来不会和工作表中的内容一起出现。The formatting is always stored separately within a single styles part for the workbook. 格式始终单独存储在工作簿的单个样式部分中。There is also a single theme part for the entire workbook.整个工作簿还有一个单独的主题部分。
A cell style can specify number format, cell alignment, font information, cell borders, and background/foreground fills. 单元格样式可以指定数字格式、单元格对齐方式、字体信息、单元格边框和背景/前景填充。Table styles specify formatting for regions of a table, such as, e.g., headers are bold or a gray fill should be applied to alternating rows. 表格样式指定表格区域的格式,例如,标题为粗体或灰色填充应应用于交替行。Pivot table styles specify formatting for regions of a pivot table, such as colors for totals or for the row axis. 数据透视表样式指定数据透视表区域的格式,例如总计或行轴的颜色。Themese define a set of colors, font information, and effects on shapes. 它们定义了一组颜色、字体信息和形状效果。A style or formatting element can define a color, font, or effect by referencing a theme, but of course that format may change if the thme is changed. 样式或格式元素可以通过引用主题来定义颜色、字体或效果,但如果主题发生更改,格式当然可能会更改。
Text-Level Formatting文本级格式
Before getting to the styles applied to a worksheet, however, let's first cover formatting at the text level, that is, not formatting applied to the entire cell, but formatting that might change from word to word, such as different colors or effects. 但是,在讨论应用于工作表的样式之前,让我们先讨论文本级别的格式设置,也就是说,不是应用于整个单元格的格式设置,而是可能因单词而异的格式设置,例如不同的颜色或效果。For example, see cell A13 below, with blue color for the first word and orange underline for the second. 例如,请参阅下面的单元格A13,第一个单词为蓝色,第二个单词为橙色下划线。Obviously this cannot be accomplished with a cell style. 显然,这不能用单元样式来完成。This formatting is done within the shared string part where the text of the cell is stored. 此格式化在存储单元格文本的共享字符串部分内完成。

Let's look at the XML for the first cell in row 13 of the worksheet part. 让我们看一下工作表部分第13行第一个单元格的XML。We know from the type attribute for the cell t="s" that the text is stored in the shared strings part, and from <v="25"/> we know that string is the 26th string or <si>. 通过单元格t="s"的type属性,我们知道文本存储在共享字符串部分,并通过<v="25"/>我们知道字符串是第26个字符串或<si>。(Remember that it is a zero-based index.)(请记住,它是一个从零开始的索引。)
The XML for the string is below. 下面是字符串的XML。Note that the formatting is applied directly within the string item, just as direct formatting is applied to text runs (<r>) using run properties (<rPr>) within wordprocessingML (docx) documents.请注意,格式设置直接应用于字符串项,就像直接格式设置使用wordprocessingML(docx)文档中的运行属性(<rPr>)应用于文本运行(<r>)一样。
Cell-Level Formatting单元级格式
Now let's return to cell styles. 现在让我们回到单元样式。Styles within spreadsheetML are implemented to minimize repetition, and this is done with collections. spreadsheetML中的样式是为了最小化重复而实现的,这是通过集合实现的。Within the styles part there are the collections shown below. 在“样式”部分中有如下所示的集合。
Most of the collections above (except for <dxfs> and <tableStyles>) relate to cells. 以上大多数集合(除了<dxfs>和<tableStyles>)都与单元格相关。And the first four--numFmts, fonts, fills, and borders--contain all of the possible charateristics for every cell in the workbook. 前四个——numFmts、字体、填充和边框——包含工作簿中每个单元格的所有可能特征。Each may have many elements, each one defining the characteristics for a set of cells that have the same such characteristics. 每个单元可能有许多元素,每个元素定义了具有相同此类特征的一组单元的特征。For example, below is a sample of the <fills> for a workbook. 例如,下面是针对工作薄的<fills>的样本。Every cell in the workbook will use one of these fill definitions.工作簿中的每个单元格都将使用其中一个填充定义。
The <fill> for a particular cell is specified with a zero-based index into the above fills collection. 这个<fill>针对一个特定的单元格,在上述集合中指定了一个从零开始的索引。The same is true of the font for the cell, the number format, and the borders. 单元格的字体、数字格式和边框也是如此。So the formatting for a cell can be specified with a list or collection of indices into these four collections. 因此,可以使用这四个集合中的索引列表或集合来指定单元格的格式。And in fact, that is what the <cellXfs> is. 事实上,这就是<cellXfs>的作用。It contains a collection of groups of indices, one group for every combination of cell formatting characteristics found in the workbook. 它包含一组索引,一组用于工作簿中找到的每个单元格格式特征组合。Below is one such grouping.下面是一个这样的分组。
Every cell will have a reference to one <xf> in the <cellXfs> collection. 每个单元格都将引用<cellXfs>集合中的一个<xf>。This is direct formatting for the cell. 这是单元格的直接格式设置。To apply a style to the cell, the <xf> references the style using the xfId attribute. 要将样式应用于单元格,<xf>使用xfId属性引用样式。The xfId attribute is an index into the <cellStyleXFs> collection, which collects the cell styles available to the user. xfId属性是<cellStyleXFs>集合中元素的索引,该集合收集用户可用的单元格样式。The <cellStyleXFs> contains one <xf> for each style. <cellStyleXFs>针对每种样式包含一个<xf>。Each such <xf> is tied to its name via an index (in its xfId attribute) from the <cellStyles> collection. 每个此类<xf>通过来自<cellStyles>集合的的索引(在其xfId属性中)与其名称绑定
Let's try and tie it all together by looking at a sample. 让我们通过查看一个样本来尝试将所有这些联系在一起。Consider row 10 in the sample below.考虑下面的示例中的行10。

The first cell A10 has a cell style applied. The XML for the cell in the worksheet is below.第一个单元格A10应用了单元格样式。工作表中单元格的XML如下所示。
From the attribute s="12" we know that the cell's formatting is stored at the 13th (zero-based index) <xf> within the <cellXfs> collection in the styles part. 从属性s="12"中,我们知道单元格的格式存储在样式表部分中的<cellXfs>集合中的第13个(从零开始的索引)<xf>中。The 13th <xf> is below.第13个<xf>如下所示。
So for this cell, the number format is the first (index value is 0) within the <numFmts> collection. 因此,对于该单元格,数字格式是<numFmts>集合中的第一个(索引位置是0)。The cell uses the font format found within the 9th <font> in the <fonts> collection, the 5th <fill> within the <fills> collection (which references a theme for the green), and the first <border> within the <borders> collection. 单元格使用在<fonts>集合中的第9个<fonts>中找到字体格式、<fills>集合中的第5个<fill>(它引用了绿色主题),以及<borders>集合中的第1个<border>。This cell also applies a style (xfId="3")--the 4th <xf> within the <cellStyleXfs> collection. 单元格还应用了一个样式(xfId="3"),即<cellStyleXfs>集合中的第4个<xf>。The style is shown below.样式如下所示。
The formatting of the style is same as the direct formatting, and the attributes applyNumberFormat, applyBorder, applyAlignment, and applyProtection, each with values of 0, tell us not to apply the corresponding values of the style but instead apply the values for the direct formatting. 样式的格式与直接格式相同,属性applyNumberFormat、applyBorder、applyAlignment和applyProtection的值均为0,它们告诉我们不要应用样式的相应值,而是应用直接格式的值。In this case they are the same, so there is no difference anyway.在这种情况下,它们是相同的,因此没有任何区别。
Table-Level Formatting表级格式
A table applies a table style by specifying a <tableStyleInfo> element within the table definition in the tables part. 表格通过指定在“表”部分中的表定义中的<tableStyleInfo>元素来应用表样式。For example, the following sample table definition specifies the TableStyleMedium9 style. 例如,以下示例表定义指定了TableStyleMedium9样式。Note that it is specified by name. 请注意,它是由名称指定的。Note also that not only is the style specified, but the specification also tells us which aspects of the style are turned on (e.g., showRowStripes="1") and which are turned off (e.g., showLastColumn="0"). 还请注意,不仅指定了样式,规范还告诉我们样式的哪些方面已打开(例如showRowStripes="1"),哪些方面已关闭(例如showLastColumn="0")。Each table style is made up of a collection of formatting definitions, each of which corresponds to a particular region of the table--e.g., whole table, first column stripe, first row stripe, first column, header column, first header cell, etc. 每个表格样式都由一组格式定义组成,每个格式定义对应于表格的特定区域,例如,整个表格、第一列条带、第一行条带、第一列、标题列、第一标题单元格等。Each of these formatting definitions can be turned on or off.每个格式定义都可以打开或关闭。
Annex G of the ECMA-376, 3rd Edition (June, 2011) OOXML specification defines built-in styles for cells, tables, and pivot tables, and style TableStyleMedium9 is among the built-in table styles. ECMA-376第三版(2011年6月)的附录G OOXML规范定义了单元格、表格和数据透视表的内置样式,样式表样式Medium9是内置表格样式之一。The built-in table and pivot table styles are not stored in the styles part--only custom styles are. 内置表格和透视表格样式不存储在“样式”部分中,只存储自定义样式。
Below is a custom style defined in the styles part, based on the TableStyleMedium9 style.下面是在“样式”部分中基于TableStyleMedium9样式定义的自定义样式。
The style looks like this:样式如下所示:

The style definition above uses differential formatting records (<dxf> elements referenced from the dxfId attribute), which enables subsets of formatting to be specified instead of specifying all formatting. 上面的样式定义使用差异格式记录(从dxfId属性引用的<dxf>元素),这允许指定格式的子集,而不是指定所有格式。Looking at the sample above, we see that the default style is the TableStyleMedium9 style. 查看上面的示例,我们看到默认样式是TableStyleMedium9样式。From that we are altering three aspects -- the wholeTable, headerRow, and firstColumn. 从这一点上,我们改变了三个方面——整体表、headerRow和firstColumn。Each of these elements references (again using a zero-based index) the <dxfs> collection within the styles part. 这些元素中的每一个都引用(同样使用一个从零开始的索引)样式部分中的<dxfs>集合。For example, the <headerRow> element references the second <dxf> (dxfId="1"). 例如,<headerRow>元素引用第二个<dxf>(dxfId="1")。It applies bold and a fill background color to the default table style defaultTableStyle="TableStyleMedium9".它将粗体和填充背景色应用于默认表格样式defaultTableStyle="TableStyleMedium9"。
Conditional Formatting条件格式
Conditional formatting is a format such as cell shading or font color that a spreadsheet can apply automatically to cells if a specified condition is true. 条件格式是一种格式,如单元格着色或字体颜色,如果指定的条件为真,电子表格可以自动将其应用于单元格。For example, you can specify that a cell fill color should be red if the value in the cell is above 50. 例如,如果单元格中的值大于50,则可以指定单元格填充颜色应为红色。It can be a very effective tool for visually highlighting important aspects of the data in a worksheet.它是一个非常有效的工具,可以直观地突出工作表中数据的重要方面。
Conditional formatting rules are stored in the worksheet part, within a <conditionalFormatting> element after the <sheetData> element. 条件格式规则存储在工作表部分中,在<sheetData>元素后面的<conditionalFormatting>元素中。The range of cells to which the formatting applies is specified with the sqref attribute. 使用sqref属性指定格式应用到的单元格范围。Each condition is within a <cfRule> element. 每个条件都在<cfRule>元素中。Multiple rules can be set, each with a different priority. 可以设置多个规则,每个规则具有不同的优先级。There are several different types of rules to specify different conditions. 有几种不同类型的规则来指定不同的条件。For example, type="cellIs" will determine a cell format based on whether a cell value is greater than or less than a specified value, or between two values. 例如,type="cellIs"将根据单元格值是大于或小于指定值,还是介于两个值之间来确定单元格格式。A type="dataBar" will display a bar of varying length within a cell based on the value in the cell. type="dataBar"将根据单元格中的值在单元格内显示长度不同的条。Theses types are set with the type attribute on <cfRule>. 这些类型是使用<cfRule>上的type属性设置的。A type="iconSet" will display an icon in the cell based upon the value in a cell. type="iconSet"将根据单元格中的值在单元格中显示图标。Below is a sample table which applies two conditions to the cells B2:B7 - one which applies a pink color fill if the value of the cell is greater than 500 and the other which applies a green fill if the value is less than 300.下面是一个示例表,它将两个条件应用于单元格B2:B7 —— 一个条件在单元格的值大于500时应用粉红色填充,另一个条件在值小于300时应用绿色填充。

The XML for the conditions is below.条件的XML如下所示。