6
$\begingroup$

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:

Manipulate[
  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}]
$\endgroup$
  • 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
9
$\begingroup$

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"}]];

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

SetDirectory[];

demo

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

xls

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

Edit

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.

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

demo2

$\endgroup$

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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