13
$\begingroup$
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.

$\endgroup$
2
  • 2
    $\begingroup$ mathematica.stackexchange.com/questions/4594/… has a hint how to do this if you use .NET. $\endgroup$ Oct 9, 2012 at 8:52
  • $\begingroup$ @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 :) $\endgroup$
    – Amatya
    Oct 16, 2012 at 7:02

3 Answers 3

4
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

Hope it's not too late... Suppose you want to dump new data (called here "datanew" and generated somehow) into a new sheet of an existing Excel file named "oldfile.xlsx" containing (in sheet 1) data ("dataold"). What I do is:

SetDirectory[NotebookDirectory[]]
dataold=Import["oldfile.xlsx"]
Export["oldfile.xlsx",{dataold,datanew}]

What you are doing is importing the data already existing and then dumping it back again in sheet 1 but, you also dump "datanew" in sheet 2. You can name each new sheet:

Export["oldfile.xlsx",{"Old Data"->dataold,"New Data"->datanew}]

(The new sheets have names: Old Data, New Data)

$\endgroup$
0
$\begingroup$

Here is a simple function which does the job for me. Just make sure that your file is in the same notebook directory.

SetDirectory[NotebookDirectory[]];
ExportExcel[FileName_, NewData_] := Module[{OldData},
 If[FileExistsQ[FileName] == True,
    OldData = Import[FileName];
    Export[FileName, AppendTo[OldData, NewData]],
    Export[FileName, NewData]
   ]
  ]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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