Simple tutorial for programmers

SYLK – SYmbolic LinK

Creating .slk spreadsheet files: cells, formulas, number formatting, fonts, and alignment

SYLK (SYmbolic LinK) is a plain-text spreadsheet interchange format with the .slk file extension. The code below was derived by saving files from Excel in SYLK format and reverse-engineering the output. It works reliably in Excel; OpenOffice support is limited. SYLK resources on the web are sparse, which is why some of the required syntax is difficult to explain.

Record structure: Every line in a SYLK file is a record beginning with a single letter followed by a semicolon. ID is the file header, C defines a cell, P defines a format or font, F applies formatting, and E marks the end of the file.

Basic cells and formulas

Each C record places a value in a cell. Y is the row number, X is the column number, and K is the cell value (quoted for text, unquoted for numbers). Formulas follow a E parameter using R1C1 notation — R1C2 means row 1, column 2.

SYLK ID;P C;Y1;X1;K"Row 1" C;Y2;X1;K"Row 2" C;Y3;X1;K"Total" C;Y1;X2;K11 C;Y2;X2;K22 C;Y3;X2;K0;ER1C2+R2C2 ← absolute formula E

Rendered spreadsheet

AB
1Row 111
2Row 222
3Total33

Relative and function-based formulas

Instead of absolute R1C1 references you can use relative addressing with bracket notation, or standard spreadsheet functions. All three variants produce the same result:

SYLK — formula variants for the total cell ← absolute reference C;Y3;X2;K0;ER1C2+R2C2 ← relative reference (one row up, two rows up) C;Y3;X2;K0;ER[-1]C+R[-2]C ← SUM function C;Y3;X2;K0;Esum(R[-1]C:R[-2]C)

The sylk1.slk example file contains all three formula types.

Number formatting

Formatting requires two steps: first, a table of format definitions using P records, then an F record to apply a format to a cell, row, or column. Format definitions are indexed from 0 — so the first P;P… line is P0, the second is P1, and so on.

The example below adds a dollar-sign format (P1) and applies it to row 3 (the total row). New lines are highlighted in green:

SYLK ID;P P;PGeneral ← P0: default format P;P_("$"* #,##0.00_) ← P1: dollar with 2 decimal places C;Y1;X1;K"Row 1" C;Y2;X1;K"Row 2" C;Y3;X1;K"Total" C;Y1;X2;K1.1 C;Y2;X2;K2.2 C;Y3;X2;K0;ER1C2+R2C2 F;P1;Y3 ← apply format P1 to row 3 E

Rendered spreadsheet (with dollar formatting on row 3)

AB
1Row 11.1
2Row 22.2
3Total$3.30

The full set of format strings produced by Excel is listed below. Any of these can be used as a P;P… record:

P;PGeneral P;P0 P;P0.00 P;P#,##0 P;P#,##0.00 P;P#,##0_);(#,##0) P;P#,##0_);[Red](#,##0) P;P#,##0.00_);(#,##0.00) P;P#,##0.00_);[Red](#,##0.00) P;P"$"#,##0_);("$"#,##0) P;P"$"#,##0_);[Red]("$"#,##0) P;P"$"#,##0.00_);("$"#,##0.00) P;P"$"#,##0.00_);[Red]("$"#,##0.00) P;P0% P;P0.00% P;P0.00E+00 P;P##0.0E+0 P;P# ?/? P;P# ??/?? P;Pm/d/yyyy P;Pd-mmm-yy P;Pd-mmm P;Pmmm-yy P;Ph:mm AM/PM P;Ph:mm:ss AM/PM P;Ph:mm P;Ph:mm:ss P;Pm/d/yyyy h:mm P;Pmm:ss P;Pmm:ss.0 P;P@ P;P[h]:mm:ss

Download sylk2.slk to see the number formatting example in action.

Bold text and font definitions

To apply bold (or any other font attribute) you must first declare a font table using P;F… records, then reference the desired font index via an F record. The font table entries use F for the font name, M for size in half-points (so M200 = 10pt), and SB for bold. The E prefix on a font record denotes an alternate encoding entry.

SYLK ID;P P;FArial;M200 ← font index 0: Arial 10pt normal P;FArial;M200 ← font index 1 P;FArial;M200 ← font index 2 P;FArial;M200;SB ← font index 3: Arial 10pt bold P;EArial;M200 ← font index 4 (alternate encoding) P;EArial;M200;SB ← font index 5: bold alternate P;EArial;M200 ← font index 6 C;Y1;X1;K"Row 1" C;Y2;X1;K"Row 2" C;Y3;X1;K"Total" C;Y1;X2;K11 C;Y2;X2;K22 C;Y3;X2;K0;ER1C2+R2C2 F;SDM4 ← apply bold (font index 4) to current cell E

Rendered spreadsheet (total cell bold)

AB
1Row 111
2Row 222
3Total33

Applying formatting to rows, columns, and alignment

The F record scope is controlled by appending RN or CN. Right-justification uses FG0R:

SYLK record Effect
F;SDM4 Bold — applies to the current cell only
F;SDM4;R3 Bold — applies to the entire row 3
F;SDM4;C2 Bold — applies to the entire column 2
F;FG0R Right-justify the current cell

Rendered — full row 3 bold + column 2 right-justified

AB
1Row 111
2Row 222
3Total33

Download sylk4.slk for a single file demonstrating all of the above — cells, formulas, number formatting, bold, and alignment.

Option records

Option records set global parameters for the workspace and should appear near the top of the file, immediately after the ID record. Two are particularly useful in practice.

A1 flag — O;L

By default, some spreadsheet applications may display grid headings numerically (row 1 / column 1, and so on) rather than in the familiar A1 letter-and-number notation. Adding O;L explicitly instructs the application to render column headers as alphabetical letters (A, B, C…) and row headers as integers (1, 2, 3…):

SYLK ID;P O;L ← force A1 column/row display C;Y1;X1;K"Column A, Row 1" C;Y1;X2;K"Column B, Row 1" C;Y2;X1;K100 C;Y2;X2;K200 E

Rendered spreadsheet (A1 notation active)

AB
1Column A, Row 1Column B, Row 1
2100200
Component Meaning
O Option record — sets a global workspace parameter
;L A1 mode flag — columns display as letters (A, B, C…), rows as integers (1, 2, 3…)

Bounds record — B;YN;XN

The B record declares the outer dimensions of the spreadsheet grid — the maximum row count (Y) and maximum column count (X). When the importing application sees this record it can immediately allocate a contiguous block of memory sized to fit the entire grid, rather than resizing dynamically as each cell record is read. For small files the difference is imperceptible, but for large datasets omitting it causes measurable load-time degradation as the parser is forced into repeated memory reallocation.

Place the B record after ID and any O records, before the first C record:

SYLK — pre-allocated bounds ID;P O;L B;Y3;X2 ← declare grid as 3 rows × 2 columns C;Y1;X1;K"Item" C;Y2;X1;K"Total" C;Y3;X2;K500 E
Component Meaning
B Bounds record — declares the grid dimensions to the parser
;YN Maximum row index in the file (e.g. ;Y100 for 100 rows)
;XN Maximum column index in the file (e.g. ;X25 for 25 columns)
If in doubt, overestimate. A B record that slightly overestimates the grid wastes a small amount of memory but parses at full speed. One that underestimates forces the same dynamic reallocation penalty as having no B record at all — the initial pre-allocated block must be discarded and rebuilt the moment a cell coordinate exceeds the declared bounds.

Example files

sylk1.slk — basic cells with all three formula variants sylk2.slk — number formatting with dollar format applied to a row sylk3.slk — font table and bold formatting sylk4.slk — complete example: cells, formulas, formatting, bold, and alignment

Related tutorials

RTF tutorial – Rich Text Format (parts 1–3)