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.

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)