# How to save the results in an identical Excel file (.xls) in each iteration of a Do loop

Assume the following code (Below code is just an example to clearly illustrate my problem):

Res = {};
Iterations = 5;

Do[
Results = AppendTo[Res, i^2]
, {i, 1, Iterations}];


I have 2 questions:

1. I want to save the results in each iteration in an identical excel file (.xls file). In the end, when the iterations are finished, I will have one Excel file with all the results in it.

2. I tried to make the code more faster and elegant using Scan[] , but I failed to do it and I couldn't do that.(It is good to mention that I am just a rookie Mathematica programmer and I don't have much experience in it!).

• I'm not sure if it's possible to export data to a pre-existing Excel file. Personally, I'd collect the results from the loop and export them at the end. Also, Do is in no way a recursive construct. Are you sure you mean recursive? Apr 30 '14 at 8:23
• the standard Export for excel files can only write content in one go. Of course you can completely overwrite the file for each line, which is inefficient but probably not too problematic. Other than that I see the following possibilities: write to a text file (or CSV) line by line, for these appending is simple and efficient. Depending on the data, you won't loose much compared to an excel file. If you really need excel, you could use NETLink to do what you want, if you search for "NET excel" you'll find many Q+A to start... Apr 30 '14 at 8:38
• @Shellp: yes, you could write a new file per iteration, but if iterations are "quick", you'll waste time for file operations.
– ciao
Apr 30 '14 at 9:46
• If one iteration takes 5 minutes and two iterations take 11 minutes then the second iteration must be taking 6 minutes. If the pattern continues (third iteration takes 7 minutes and so on) the total time should be over 24 hours. So the question is why does each iteration take longer than the last? We can't answer that without knowing what you're actually doing. Apr 30 '14 at 16:09

Here are a couple of schemes suggested in the comments, plus one of my own, namely, to save the results every ten minutes or whatever:

(* save after every iteration *)
Do[
Results = AppendTo[Res, i^2];
Export["/tmp/thesamefile.xls", Results],
{i, 1, Iterations}];

(* save after ten minutes *)
time = AbsoluteTime[];
Do[
Results = AppendTo[Res, i^2];
If[AbsoluteTime[] - time > 600,    (* 600 sec = 10 min *)
Export["/tmp/thesamefile.xls", Results];
time = AbsoluteTime[]],
{i, 1, Iterations}];
Export["/tmp/thesamefile.xls", Results];

(* all results at the end *)
Do[
Results = AppendTo[Res, i^2],
{i, 1, Iterations}];
Export["/tmp/thesamefile.xls", Results];


If the result of each iteration consists of a large amount of data, one might be better off writing to a different file each time; see for instance, How to export a large number of files?