XML technologies / SpreadsheetML / SpreadsheetML
SpreadsheetML
This article explains how content parts represent spreadsheets in Excel documents. Spreadsheets are Excel’s core function; their parts use SpreadsheetML, described in the first part of the OOXML standard.
The following parts are required for spreadsheets:
/xl/workbook/sheet[i].xml
/xl/styles.xml
/xl/workbook.xml
Additional parts may be added:
/xl/sharedStrings.xml
/xl/calcChain.xml
A spreadsheet model must support merged cells, different cell styles and typed cell contents. Row- and column-wide settings such as width, height and styles are also required. Spreadsheets additionally have formulas calculating cell values.
Workbook
The workbook part mainly references and names worksheets, sets their order and stores general document settings. The following example shows its most basic version:
<workbook>
<sheets>
<sheet name="Tabelle 1" sheetId="1" r:id="rId1"/>
<sheet name="Tabelle 2" sheetId="2" r:id="rId2"/>
<sheet name="Tabelle 3" sheetId="3" r:id="rId3"/>
</sheets>
</workbook>The <sheets> element has a <sheet> child for every worksheet. The order of <sheet> elements determines the sheet order in Excel. A <sheet> element receives its worksheet name through the name attribute, a unique ID through the sheetId attribute and a relationship from the associated relationship part through the r:id attribute. That relationship points to the worksheet’s sheet part.
Worksheet
To begin, we will represent the simple spreadsheet in Figure 4 using SpreadsheetML.
Fig. 4: a simple Excel spreadsheet
The following code shows the part /xl/worksheets/sheet1.xml containing this spreadsheet’s contents:
<worksheet>
<sheetViews>
<sheetView workbookViewId="0"/>
</sheetViews>
<sheetData>
<row r="1">
<c r="A1" t="s">
<v>1</v>
</c>
<c r="B1" t="s">
<v>2</v>
</c>
</row>
<row r="2">
<c r="A2">
<v>1</v>
</c>
<c r="B2">
<v>1</v>
</c>
</row>
<row r="3">
<c r="A3">
<v>2</v>
</c>
<c r="B3">
<v>4</v>
</c>
</row>
[...]
<row r="11">
<c r="A11">
<v>10</v>
</c>
<c r="B11">
<v>100</v>
</c>
</row>
</sheetData>
</worksheet>This example introduces SpreadsheetML’s basic elements.
Basic structure
Besides metadata, the root <worksheet> contains the sheet structure in <sheetData>. The content of <sheetData> consists of table rows. Since Excel sheets have over one million empty rows, only rows with data are stored. An empty sheet therefore has an empty <sheetData>.
Table row
A row is represented by a <row> element:
<row r="3">
[Tabellenzellen dieser Zeile]
</row>The r attribute specifies the row number when different from automatic numbering. This is therefore the third row. If there are no <row> elements with the attributes r="1" and r="2", the first two rows remain empty. Automatic numbering derives from position in <sheetData>. If the <row> element has no row number assigned through the r attribute, its number is one more than the previous row’s. Without a previous row, the automatic number is 1.
Table cell
Cells follow the same principle as rows: empty cells are omitted. Cells use <c> elements:
<c r="B3">
[Inhalt der Tabellenzelle]
</c>
Fig. 5: a spreadsheet cell name
Here, the r attribute specifies the Excel cell name. B3 combines column B and row 3. Each <c> row number must match the <row> containing that <c>, even when the <row> number is determined automatically.
As with <row>, the <c> element’s r attribute is optional. Automatic naming works similarly: the first unnamed <c> belongs to column A; later unnamed cells derive their column from the preceding cell.
Cell contents
SpreadsheetML distinguishes cell content types structurally. The simplest is Number; others include strings and Boolean values. A <c> element receives a t attribute to assign its content type. The following sections show these representations.
Type: Number
Since Excel primarily handles numbers, the numeric type Number is the default cell type. Cells without a t attribute are therefore numeric. This type can also be assigned explicitly using the value n for the t attribute:
<c r="B3" t="n">
<v>4</v>
</c>
Numeric cells have a <v> child containing a decimal number, using a dot as decimal separator:
<v>4.5</v>Type: String
Excel offers two string representations. By default, it uses the more complex option, collecting all strings in the sharedStrings part in an unordered collection shared by all worksheets. Cell references associate each cell with a string. Since this work aims to transform XML into XLSX, generically generating a sharedStrings part would require considerable effort. SpreadsheetML provides a much simpler string representation avoiding this effort. We therefore do not describe the sharedStrings part here.
Instead, we show how to write strings directly into cells using the type inlineStr as the value of the t attribute. A cell of this type must have an <is> child inside <c>. For a plain string without inline formatting, <is> contains a <t> element holding the string:
<c r="A1" t="inlineStr">
<is>
<t>x</t>
</is>
</c>
Inline markup
Formatting only part of a cell’s string is rare in spreadsheets. Since the principle comes from WordML, we refer only to the article WordML - Inline elements.
Type: Boolean
For a Boolean cell value, the t attribute must have the value b. As with numeric cells, a <v> child holds the value, but only 1 (logical TRUE) or 0 (logical FALSE) is allowed:
<c r="A1" t="b">
<v>1</v>
</c>Spanning cells
Spreadsheet models must handle cells spanning columns or rows. Merging neighbours in a row spans columns; merging within a column spans rows. Only the topmost or leftmost cell is considered the spanning cell, covering cells to its right or below in European writing direction. A cell may span both rows and columns.
How these properties are described depends on the table model. Compared with common models, Excel’s implementation is unusual.
Most models attach spanning information to the cell itself, but SpreadsheetML stores it separately. The top-level <mergeCells> element of the sheet part, immediately following <sheetData>, contains one <mergeCell> per merged area.
<sheetData> [...] </sheetData>
<mergeCells>
<mergeCell ref="A2:B11"/>
</mergeCells>The ref attribute of <mergeCell> specifies a range including the spanning cell and every covered cell.
Excel defines a cell range using two cell names separated by a colon (Zellenname1:Zellenname2), defining a rectangle from the top-left to the bottom-right cell. A2:B11 includes A2, B2, A3, B3, [usw.] through A11 and B11.
When merging, Excel ignores whether covered cells are already defined or contain data. Only the spanning cell’s content is displayed.
<sheetData>
<row>
<c r="A1" t="inlineStr">
<is>
<t>x</t>
</is>
</c>
<c t="inlineStr">
<is>
<t>y</t>
</is>
</c>
</row>
[…]
</sheetData>
<mergeCells>
<mergeCell ref="A1:B1"/>
</mergeCells>
Fig. 6: a spanning cell
Here A1 spans B1, hiding its content y. This happens even though the cell has no r attribute assigning an explicit name, showing that automatic cell naming ignores spans.
To display the covered cell in column C, give it the explicit name C1 or insert an empty cell between the two.
Formulas
Formulas are essential to spreadsheets, usually calculating a cell’s value from other cells. Excel recognises a formula when input begins with =. Formula construction itself is outside this discussion. SpreadsheetML stores the same formula without the equals sign, except that function names must always be English when Excel uses another language (for example AVERAGE() instead of MITTELWERT()).
f element
Consider =A3*A3. The <c> element of a cell holding its result receives an <f> child containing that formula:
<c r="B3">
<f>A3*A3</f>
</c>For better performance, the resulting value may also be written into <c>:
<c r="B3">
<f>A3*A3</f>
<v>4</v>
</c>Shared formulas
Often a column’s cells need the same context-dependent formula, such as squaring the neighbouring cell. In Excel, enter it in the first cell, copy that cell and paste into the remaining cells (Figure 7).
Fig. 7: creating shared formulas in Excel
Copying is relative: the formula is adjusted to preserve relationships between referenced cells and the cell receiving the copy.
SpreadsheetML follows this approach. One cell contains the formula; others reference it to reuse its relative relationships. A shared formula carries the attribute t="shared". In addition, the si attribute supplies an ID, while the ref attribute specifies the range where references to the formula may appear:
<row r="2">
[...]
<c r="B2">
<f t="shared" ref="B2:B11" si="0">A2*A2</f>
</c>
</row>Formula references are allowed only within B2:B11, but could occur only in B3. The range may therefore be larger than strictly necessary.
To reference it from a cell in this range, add an empty <f> child to <c>, also carrying the attribute t="shared" and an si attribute with the referenced ID as its value.
<row r="3">
[...]
<c r="B3">
<f t="shared" si="0"/>
</c>
</row>The calcChain part
The formula descriptions so far in the sheet part are insufficient to produce an error-free document. Excel also requires, in the calcChain part, a list of every calculated cell in the document. Rather than an XML hierarchy, this is a sequence of <c> elements. Each listed cell has a <c> element with an r attribute giving the cell name:
<calcChain>
<c r="B2" i="1"/>
<c r="B3"/>
<c r="B4"/>
<c r="B5"/>
[...]
</calcChain>Sheet changes are represented by <c> element order. The first <c> referring to a new sheet also receives an i attribute with the sheet ID specified in the workbook part.
<c r="B2" i="1"/>
<c r="B3"/>
[...]
<c r="A2" i="2"/>
<c r="C15"/>
[...]Styles
The styles part allows different cell formatting. With virtually countless options, only these layout features will be described:
Typeface and font style
Font and background colour
Cell borders
Number formats
Alignment
A style is defined in the top-level <cellXfs> element of the styles part using an <xf> element. Each differently formatted cell needs a style. To assign one, add an s attribute to <c>:
<c r="B3" s="1">
[...]
</c>The attribute value is the index of an <xf> within <cellXfs>. Indices of <xf> start at 0, so this example assigns the second style.
<cellXfs>
<xf fontId="0" fillId="0" borderId="0"/>
<xf fontId="1" fillId="2" borderId="0"/>
</cellXfs>The <xf> element in turn references formatting components through attributes, including the font (fontId attribute), background colour (fillId) or border formatting (borderId). These references also use container-element indices, pointing to top-level <fonts>, <fills> and <borders> containers holding the respective formatting definitions.
Font style
In <fonts>, a <font> element defines a font style. A typeface groups several styles distinguished primarily by family, weight, slant and size. Each supported weight generally has regular and italic styles; fonts often also have special designs for very small sizes.
The <font> element defines the font style through these children:
<name> | Typeface |
<sz> | Font size |
<i> | Italic |
<b> | Font weight |
This is only a selection of the possible children of <font>.
These elements are always empty, each with a val attribute containing its formatting value: the full font name for <name>, a decimal point size for <sz>, and Boolean italic or bold flags for <i> and <b>.
Excel supports font sizes only in half-point steps, so 11.7 is rounded down to 11.5.
The <i> and <b> elements need not have a val attribute because true is the default. Only disabling italic or bold requires the val attribute with the value false. Weights other than bold are unsupported.
<fonts>
<font>
<b/>
<sz val="11"/>
<name val="Times New Roman"/>
</font>
<font>
<i/>
<sz val="10"/>
<name val="Verdana"/>
</font>
</fonts>
[...]
<cellXfs>
<xf fontId="1" fillId="0" borderId="0"/>
<xf fontId="0" fillId="2" borderId="0"/>
</cellXfs>This example uses Times New Roman, 11 pt, bold, and Verdana, 10 pt, italic. The two styles in <cellXfs> use the index, determined by <font> order, in their respective fontId attribute to reference these <font> elements. The first <xf> references Verdana; the other references Times New Roman.
Font colour
<font> can also contain formatting not intrinsic to a font style, such as text colour through the <color> element. Styles sharing a font style but differing in colour must reference different <font> elements.
The <color> element accepts an ARGB value through the rgb attribute. This eight-digit hexadecimal number represents four consecutive two-digit values, each ranging from a maximum of 255 (FF) to a minimum of 0 (00). The first is alpha, indicating opacity: FF means 100% opacity, fully opaque; 00 means 0% opacity. The last three values are red, green and blue. Excel ignores alpha for text and background colours and displays all colours fully opaque.
This <font> example defines Verdana, italic, 10 pt, with yellow text:
<font>
<i/>
<sz val="10"/>
<name val="Verdana"/>
<color rgb="FFFFFF00"/>
</font>Background colour
Unlike text colour, background colour is independent of <font> and stored in <fills>. Two <xf> styles may reference the same <font> but have different backgrounds.
As with fonts, the index in the fillId attribute of <xf> references a <fill> inside <fills>.
A <fill> contains either <patternFill> for patterns, solid or absent backgrounds, or <gradientFill> for gradients. We describe only <patternFill>, since gradients are rarely used in Excel.
The <patternFill> element receives a patternType attribute identifying a solid or transparent background or a specific pattern. Transparency uses none, a solid colour uses solid. The standard lists further values for specific patterns.
Colours appear inside <patternFill>. Patterns distinguish background <bgColor> and foreground <fgColor>. Solid fills use only <fgColor>. Apart from that distinction, these work like the text-colour <color> element.
This <fill> produces a solid yellow background:
<fill>
<patternFill patternType="solid">
<fgColor rgb="FFFFFF00"/>
</patternFill>
</fill>Cell borders
Like fonts and backgrounds, borders are referenced from <xf> through the borderId attribute, pointing to a <border> element in <borders>.
<border> contains an element for each side and the diagonal: <left> (left), <right> (right), <top> (top), <bottom> (bottom) and <diagonal> (diagonal).
Each may contain a <color> element and have a style attribute. A border is visible only when <color> assigns its colour, as for text, and the style attribute defines its line style. There are 14 predefined styles:
Fig. 8: Excel border-style options (see Table 2)
Table 2: attribute values for border styles (see Fig. 8)
Attribute values for the style attribute | |
|---|---|
none | mediumDashDotDot |
hair | slantDashDot |
dotted | mediumDashDot |
dashDotDot | mediumDashed |
dashDot | medium |
dashed | thick |
thin | double |
For a dotted blue bottom border, assign this border style:
<border>
<bottom style="dotted">
<color rgb="FF0000FF"/>
</bottom>
</border>Number formats
Numeric cells can display values as currency, integers or decimals with at least three decimal places, for example. A number format assigned to the cell’s <xf> style determines the display.
Again, an attribute of <xf> references the detailed definition in a container. Number formats use the attribute numFmtId to reference <numFmt> in <numFmts>. For no apparent reason, SpreadsheetML uses a freely chosen ID here rather than an index.
Since <numFmt> is not referenced by index, each <numFmt> needs an ID through its attribute numFmtId. A string in the attribute formatCode specifies the number format.
Format string
Formatting rules interpret special characters in a string as a number-display pattern. The following examples explain the most important characters.
Fixed digits
Table 3 shows values displayed with the pattern 00.0:
Value | Display |
|---|---|
0.10 | 00,1 |
1 | 01,0 |
5.555 | 05,6 |
=1/3 (formula) | 00,3 |
115 | 115,0 |
One decimal place is always displayed. The pattern’s dot represents the application’s decimal separator, here a comma according to Excel settings. The character 0 requires a digit, padding with zeroes where the value has insufficient digits before or after the separator. Extra integer digits are still displayed; excess decimal places are rounded conventionally to the pattern’s precision.
Optional digits
Besides 0, the hash sign (#) represents an optional digit. Table 4 illustrates the pattern #0.0#:
Value | Display |
|---|---|
0.10 | 0,1 |
1 | 1,0 |
5.55 | 5,55 |
=1/3 (formula) | 0,33 |
115 | 115,0 |
5.555 | 5,56 |
Unlike 0, # is never padded with a zero. If the value lacks sufficient precision or magnitude, that position disappears. The displayed digit count therefore varies with the value.
Literal characters
Quotation marks enclose a literal string. Table 6 illustrates the pattern "betrag:" 0.0 €:
Value | Display |
|---|---|
1 | betrag: 1,0 € |
50 | betrag: 50,0 € |
The pattern is an attribute value, where double quotes " cannot appear directly. Other XML languages often substitute single quotes ('), but this is invalid in the pattern. Double quotes must therefore be replaced by the entity ":
<numFmt numFmtId="0" formatCode="&quot;betrag:&quot; 0.0 "/>Some characters are inserted literally without escaping: currency symbols such as € and $, spaces, plus (+), minus (-) and parentheses.
Other individual literal characters can be escaped with a preceding backslash (\). The subpattern \e inserts a literal "e".
Negative values
Two subpatterns separated by a semicolon (;) distinguish positive and negative values. The first applies to positives, the second to negatives. Table 6 shows the pattern \P\: 0.00 €;\N\: 0.00 €:
Value | Display |
|---|---|
10 | P: 10,00 € |
-10 | N: 10,00 € |
0 | P: 0,00 € |
Two more semicolon-separated subpatterns may be appended:
positiverWert;negativerWert;nullWert;stringMuster.
The third handles zero, otherwise treated as positive; the fourth displays a string entered instead of a number.
Alignment
Unlike previous formatting features, cell alignment is defined directly in <xf> through its <alignment> child, also supporting text rotation and writing direction. Alignment uses a horizontal or vertical attribute. Horizontal alignment supports center (centred), general (depending on cell type), justify (justified), left (left-aligned) and right (right-aligned). These are only the most common horizontal options.
Vertical alignment supports, among others, center (middle), top (top), bottom (bottom) and justify (justified) as values of the vertical attribute.
Summary
Fig. 9: required header-cell formatting
To summarise these features, we define a header-cell style for Figure 9 with these properties:
Blue right and bottom borders
Yellow background
Font style:
Typeface: Verdana
Font size: 10 pt
Bold
Font colour: blue
Alignment
Vertical: top
Horizontal: centred
<fonts>
<font> [...] </font>
<font>
<b/>
<sz val="10"/>
<color rgb="FF0000FF"/>
<name val="Verdana"/>
</font>
</fonts>
<fills>
<fill>
<patternFill patternType="solid">
<fgColor rgb="FFFFFF00"/>
</patternFill>
</fill>
</fills>
<borders>
<border/>
<border>
<right style="medium">
<color rgb="FF0000FF"/>
</right>
<bottom style="medium">
<color rgb="FF0000FF"/>
</bottom>
</border>
</borders>
[...]
<cellXfs>
<xf fontId="0" fillId="0" borderId="0"/>
<xf fontId="1" fillId="0" borderId="1">
<alignment horizontal="center" vertical="top"/>
</xf>
[...]
</cellXfs>The second <xf> defines the header, referencing the second <font> in <fonts>, first <fill> in <fills> and second <border> in <borders>. Its <alignment> centres horizontally and aligns to the top vertically.
This article covers only a selection of common formatting properties that the styles part assigns to cells.
Formatting entire rows
Some formatting applies to an entire row, such as row height, which cannot differ by cell.
Common table models also allow row-wide formatting inherited by cells without their own style. Uniform formatting can then be set centrally rather than cell by cell.
Row height
Fig. 10: row height
To specify a custom row height, give <row> a customHeight attribute with the value 1 or true. The row height can then be specified using the ht attribute, in points.
<row r="1" customHeight="1" ht="21">
[...]
</row>Row styles
SpreadsheetML unfortunately supports row-wide cell formatting only partially. A row style applies only to cells not represented by <c>, meaning empty cells. Uniform formatting therefore requires assigning the same style to <row> and every <c>.
Fig. 11: row style
Like <c>, <row> receives its style through the s attribute. The <row> also needs an indicator of manual formatting: the customFormat attribute, whose value 1 or true enables manual formatting:
<row r="1" s="2" customFormat="1">
<c r="A1" s="2" t="inlineStr">
[...]
</c>
<c r="B1" s="2" t="inlineStr">
[...]
</c>
</row>The s attributes on both <c> elements are necessary; without them, these cells would have no formatting.
Formatting entire columns
Columns also need formatting, but no element contains all a column’s cells. SpreadsheetML follows common table models: <col> elements in a <cols> container define columns. <cols> is a top-level element of the sheet part immediately before <sheetData>:
<worksheet>
[...]
<cols>
[Enthält mit col-Elementen Angaben zu den Spalten]
</cols>
<sheetData>
[...]
</sheetData>
[...]
</worksheet>Not every formatted column needs its own <col>. One <col> can cover several columns using the <col> attributes min and max, whose indices specify the range covered by this <col>.
<col min="2" max="4"/>This <col> may cover columns 2 (B), 3 (C) and 4 (D). If ranges of different <col> elements overlap, the last <col> in <cols> takes precedence.
<cols>
<col min="2" max="4"/>
<col min="3" max="5"/>
</cols>The first <col> covers column 2; the second <col> covers columns 3, 4 and 5.
Column width
Fig. 12: column style
Like row height, column width is specified centrally using the attributes customWidth and width. The customWidth attribute indicates a manually specified width (with 1 or true). The width appears in the width attribute, in a unit intended to count digits fitting without overlap. The calculation appears to assume only Calibri at 11 pt.
Column styles
Like rows, columns may have a style for empty cells. On <row>, the s attribute corresponds on <col> to the style attribute. Again, a style from the styles part is referenced by index. Unlike row styles, no indicator of manual formatting is required here.
<cols>
<col min="1" max="1" style="1"/>
<col min="2" max="2" style="5"/>
</cols>
<sheetData>
<row r="1">
<c r="A1" t="inlineStr" s="1"><is><t>x</t></is></c>
<c r="B1" t="inlineStr" s="5"><is><t>y</t></is></c>
</row>
<row r="2">
<c r="A2" s="1"><v>1</v></c>
<c r="B2" s="5"><v>1</v></c>
</row>
[...]
</sheetData>
|