Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.
m = Table[i - j, {i, 5}, {j, 6}]
n = Table[i - j, {i, 3}, {j, 7}]
o = Table[i^j, {i, 3}, {j, 7}]
Export["mfile2.xls", {"m Data" -> Transpose[m],"n Data" -> Transpose[n]}]

The above code creates a spreadsheet with two sheets named "m-data" and "n-data". Now I would like to be able to add more data to the existing spreadsheet. But when I do this:

Export["mfile2.xls", {"o Data" -> Transpose[o]}]

The earlier data is deleted and now my mfile2.xls just has the one sheet names "o-data" containing o-data.

Is it possible for me to Export data into some particular existing sheet and maybe even a specific cell of some pre-existing excel file and/or maybe get MMA to create a new sheet in addition to the existing ones.

share|improve this question
2  
mathematica.stackexchange.com/questions/4594/… has a hint how to do this if you use .NET. –  Sjoerd C. de Vries Oct 9 '12 at 8:52
    
@SjoerdC.deVries I spent a bit of time trying to mimic the .NET but I couldn't get it to work. Thanks for linking to it. I'm not very far from getting it. Whatever I am doing, does open the right excel file but as of now closes it right back. I'll look at it again in a few months when I have more time. Right now I have just switched to saving data as "*.dat" Thanks :) –  Amatya Oct 16 '12 at 7:02
add comment

1 Answer

Maybe this approach is not sophisticated enough, but it may come handy if you want to add only a few new sheets to your table or you want to update it regularly. Given you have created your data "m, n" and have exported it to "mfile2.xls".

You import the data of your XLS-file

data = Import[NotebookDirectory[] <> "mfile2.xls"];

and import the names of your sheets

sheets = Import[NotebookDirectory[] <> "mfile2.xls", "Sheets"]

Now you create your new data "o".

o = Table[i^j, {i, 3}, {j, 7}]

Then you rebuild the structure that you have exported earlier

olddata = sheets[[#]] -> data[[#]] & /@ Range[Length[sheets]]

and you add the data of your new sheet to it

newdata = Append[olddata, "o Data" -> Transpose[o]]

Finally you export the updated data to the original file

Export[NotebookDirectory[] <> "mfile2.xls", newdata]

which gives you an XLS containing all previous sheets along with the new one.

For convenience you can build yourself a function to which you only pass the filename of the XLS you want to append a new sheet to and the new data.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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