I am trying to generate a table with Manipulate and export the results into an MS Excel file.

Fortunately, after a lot of troubles, I was able to get the file export right but the headings were not included in the Excel file. I would be quite grateful for you guys to point to me where I am getting it wrong. Please see my code below:

  nino = 
    TableForm[Table[(a*i)^2 - j^3, {i, 5}, {j, 6}], 
      TableHeadings -> {None, {n, n^2, n^3, k, g, t}}], 
  Button["Export", Export["ndata" <> ToString[a] <> ".xls", nino]], 
  {nino, ControlType -> None}, 
  {a, 0, 2}]
  • 1
    $\begingroup$ TableForm is only a display form. Instead of using it just export your list with table headings attached, i.e.: Export[..., Prepend[Table[(a*i)^2 - j^3, {i, 5}, {j, 6}], {n, n^2, n^3, k, g, t} ]]. Also what's with the Manipulate here? It doesn't seem to do much of anything that couldn't be better done programmatically. $\endgroup$ – b3m2a1 Jan 14 at 1:47

Exporting to Excel means that will have forego the use of the TableHeadings option of TableForm. You can do it like this.

dir = SetDirectory[FileNameJoin[{$HomeDirectory, "Desktop"}]];

  {nino =
     Join[{{n, n^2, n^3, k, g, t}}, Table[(a*i)^2 - j^3, {i, 5}, {j, 6}]];
     Export[FileNameJoin[{dir, "ndata" <> ToString[a] <> ".xls"}], nino],
     ImageSize -> Automatic,
     Method -> "Queued"]}],
  {nino, None},
  {a, 0, 2, .1, Appearance -> "Labeled"}]



Then clicking on the export button will produce an .xls file that looks like this:


BTW: Having negative numbers under a column headed $n^2$ is strange


I am going to recant on my assertion that Exporting to Excel means that will have forego the use of the TableHeadings option of TableForm. It is a little more work to use the option, but it does result in a better looking Manipulate output.

    {data = Join[{lbls}, Table[(a*i)^2 - j^3, {i, 5}, {j, 6}]];
     TableForm[Rest @ data, TableHeadings -> {None, lbls}], 
       Export[FileNameJoin[{dir, "ndata" <> ToString[a] <> ".xls"}], data], 
       ImageSize -> Automatic,
       Method -> "Queued"]},
    Dividers -> {False, 2 -> True}],
  {data, None},
  {{lbls, {n, n^2, n^3, k, g, t}}, None},
  {a, 0, 2, .1, Appearance -> "Labeled"}]



Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.