# Run nb file and export each time in new line (of the same .xls file)

I have a problem that really limits my productivity. I need to run a script several times, export results in an .xls file. Instead of manually fullfilling my results in my .xls file, I thought about exporting my results in an excel file and just copy them. I know how to export my results each time in an excel file (this will overwrite my previous file each time, so I will have to copy results before rerunning script), I know how to automatically change the name of my exported file (receiving a unique ID so that I can then copy all of my exported files into my desired one), but the optimal solution would be to run my script, store results in an .xls file, and then rerunning it, having new values stored in previous files new line etc.

I tried many solutions found scatted in the web but nothing works, my exported file always remains in tacked, or gets corrupted (I tried PutAppend, Openwrite and other solutions, none of them worked , i guess I did something wrong).

Here is a dummy script:

x1=1;
f[x_] := PDF[PoissonDistribution[2.5], x];
myExportLine = {{"Text",x1, f[x1]}};

Export["Test_file.xls", myExportLine, "XLS"]


I need to provide values for my script manually (meaning I do not need to run any loop for my x1 values. Any idea how to implement it?

Example of fail attempt:

f1=OpenAppend["Test_file.xls"]

x2=2;
f[x_]:=PDF[PoissonDistribution[2.5],x];
myExportLine2={{"Text",x2,f[x2]}};
WriteString[f, myExportLine2, "\n"];
Close[f1];


edit: I corrected the repentance of f in two different variables. Renamed one of them into f1.

• WriteString writes that string literally and doesn't know anything about formats. An Excel file has a complicated internal structure. If you just append some arbitrary bytes at the end, you will corrupt the file. Dec 6, 2016 at 14:03
• How about storing the whole list im memory (in Mathematica) and saving it just once? myExport = Table[{StringTemplate["Test: "][x1], x1, f[x1]}, {x1, 0, 2, .1}]; Export["Test_file.xls", myExport, "XLS"], or do you want to close Mathematica in between?
– grbl
Dec 6, 2016 at 14:04
• Szabolcs thank you for clarifying that to me. grbl this method keeps overwriting on my previous results. I do not need to close mathematica, but I need to quit kernet at each run. Dec 6, 2016 at 16:25
• aside to your question, using the symbol f for two completely different things is making a mess here. Dec 6, 2016 at 16:34
• @george2079 yes you are right, I misstyped while providing my example. Originally i used f for my function solely. Thank you for pointing that out Dec 7, 2016 at 14:13

It is not possible to append to an Excel file. The only way is to read the data into memory, Append the additional data in-memory, the re-export the whole thing to disk. This is of course inefficient and it is up to you to decide if it is suitable for your use case.