ooxml

examples
Login

examples

Examples


Reading a complete XLSX workbook including all sheets:

package require ooxml

array set workbook [ooxml::xl_read my-first.xlsx]

Generating XLSX with direct addressing with -index:

package require ooxml

set spreadsheet [::ooxml::xl_write new -creator {Creator Name}]
if {[set sheet [$spreadsheet worksheet {Sheet 1}]] > -1} {
  $spreadsheet cell $sheet 12.34 -index 0,0
  $spreadsheet cell $sheet 5.6 -index 0,1
  $spreadsheet cell $sheet {} -index C1 -formula {A1+B1}

  $spreadsheet cell $sheet {My Text} -index A2 -string
}
$spreadsheet write my-first.xlsx
$spreadsheet destroy

You can use numeric index row,col (0,0) or alfanumeric index like Excel does rowcol (A1).

An other way is using autoincrement:

package require ooxml

set spreadsheet [::ooxml::xl_write new -creator {Creator Name}]
if {[set sheet [$spreadsheet worksheet {Sheet 1}]] > -1} {
  $spreadsheet row $sheet
  $spreadsheet cell $sheet 12.34
  $spreadsheet cell $sheet 5.6
  $spreadsheet cell $sheet {} -formula {A1+B1}

  $spreadsheet row $sheet
  $spreadsheet cell $sheet {My Text} -string
}
$spreadsheet write my-first.xlsx
$spreadsheet destroy

Rows and coulmns are auto incremented and can be skipped by -index like this:

$spreadsheet row
$spreadsheet cell $sheet Value1
$spreadsheet cell $sheet Value2 -index 4
$spreadsheet cell $sheet Value3
$spreadsheet row -index 3
$spreadsheet cell $sheet Value4

The result will be Value1 in 0,0 (A1), Value2 in 0,4 (E1), Value3 in 0,5 (F1) and Value4 in 3,0 (A4).

There are two helper to convert row,col to string and the other way around.

::ooxml::StringToRowColumn A1

Result = 0,0

::ooxml::RowColumnToString 0,0

Result = A1

An other helper can calculate the column width of a cell:

::ooxml::CalcColumnWidth 16

Result = 16.7109375


XLSX Example Files


xlsx-writeRowsIndex.tcl:

Write an Excel file method one: direct addressing with -index.
Output: writeRowIndex.xlsx


xlsx-writeRowsAutoIncr.tcl:

Write an Excel file method two: with row auto increment.
Output: writeRowsAutoIncr.xlsx


xlsx-tablelist.tcl:

Exporting a Tablelist to an Excel file.
Output: tablelist.xlsx


xlsx-formula.tcl:

Write an Excel file with a formula A1+B1. Added shared formula (v >= 1.6).
Output: formula.xlsx


xlsx-defaultDateStyle.tcl:

Write an Excel file with defaultdatestyle set.
Output: defaultDateStyle.xlsx


xlsx-aLotOfFormats.tcl:

Write a complex Excel file with a lot of formats.
Output: aLotOfFormats.xlsx


xlsx-complexReadWrite.tcl:

Read a complex Excel file (generated by Excel) interpret all known formats and write it back.
Input: original_excel.xlsx
Output: complexReadWrite.xlsx


xlsx-searchAndReplace.tcl:

Read in an Excel file search and replace some cells and write a new file.
Input: form8.xlsx
Output: searchAndReplace.xlsx


xlsx-hyperlinksTooltips.tcl:

Write an Excel file with hyperlinks and tooltip. (v >= 1.8)
Output: hyperlinksTooltips.xlsx


xlsx-printArea.tcl:

Write an Excel file with some data and set print areas on sheet 1, 2 and 4. (v >= 1.10)
Output: printArea.xlsx


DOCX (v >= 1.11)

Creating a Word document works with the ooxml::docx package. A minimal "Hello, World" looks like this:

package require ooxml::docx

set docx [ooxml::docx::docx new]
$docx paragraph "Hello, World!"
$docx write hello.docx
$docx destroy

For the full method and option reference see man-docx.


DOCX Example Files


docx-example1.tcl:

Basic document: a default style, several paragraphs, a hyperlink, a comment and a textbox.
Output: docx-example1.docx


docx-example2.tcl:

Headers and footers (using the returned rId), a page field, an image and a textbox.
Output: docx-example2.docx


docx-example3.tcl:

Document settings, a simple table and a complex table, and an image.
Output: docx-example3.docx


docx-example4.tcl:

Start from an existing .docx and import its styles.
Input: docx-example4-in.docx
Output: docx-example4.docx


docx-example5.tcl:

Import a numbering definition from another .docx and use a numbered list.
Input: docx-example4-in.docx
Output: docx-example5.docx


docx-example6.tcl:

Inline and anchored images.
Output: docx-example6.docx


docx-example7.tcl:

Document fields (page number, number of pages, date, etc.).
Output: docx-example7.docx


docx-example8.tcl:

Footnotes.
Output: docx-example8.docx


docx-example9.tcl:

Math formulas (OMML), inline and as display equations.
Output: docx-example9.docx


docx-example10.tcl:

Footnotes with several output files written from one document.
Output: docx-example10.docx, docx-example10-1.docx, docx-example10-2.docx


docx-example11.tcl:

Comments, including comment ranges.
Output: docx-example11.docx


docx-example12.tcl:

A larger document combining headers and footers, tables, footnotes, endnotes, comments, fields and a textbox.
Output: docx-example12.docx