Exporting Tables in TableForm

If you wish to Export a table from Mathematica and maintain the table formatting you will need to use a few tricks. This notebook illustrates how to handle the problem.

Some Data

data={{Sec[1],Sec[2],Sec[3]},{((Sec[1])/2),((Sec[2])/2),((Sec[3])/2)},{((Sec[1])/3),((Sec[2])/3),((Sec[3])/3)},{((Sec[1])/4),((Sec[2])/4),((Sec[3])/4)}}

And a table

Here we put our data in TableForm.

tbl1 = TableForm[data,TableSpacing -> {4,6},TableHeadings -> {{"A","B","C","D"},{"I","II","III"}}]

If we try to export this we either get an error message or an un-formatted table.

Export["tableOne",tbl1,"JPEG"];

Export["tableOne",tbl1,"Table"]

You can check using a text editor that the above file contains the data but none of the formatting.

The trick to keeping the formatting

The trick here is to use the cell data and note the table. You can see what we will export by selecting the cell bracket of the table above and choosing ShowExpression from the Format menu. Here is the quick and tidy way to create the same information.

tbl2=Cell[BoxData[ToBoxes[TableForm[data,TableSpacing->{4,6},TableHeadings->{{"A","B","C","D"},{"I","II","III"}}]]],"Output"];

We export the cell data using export. Since the window size will often clip the right side of the table we include an option value to make sure the entire table is included in the export. Notice that we also suppress the export of the cell bracket.

Export["tabletwo.jpg",Notebook[{tbl2},WindowSize->500,ShowCellBracket->False],ImageSize->6 72]

If you open the above image in a browser you will see the table has been exported in a form which preserves all the formatting you see in a Mathematica notebook.