I'm importing financial data into Mathematica, creating column headers for each array and exporting to Excel. The code below accomplishes most of this, but it creates a different worksheet for each array. I want to combine multiple arrays onto a single worksheet, one per column with the header at the top. Suggestions? Thanks.

spy = Prepend[FinancialData["SPY", "Jan.1,2012"], {, "SPY"}];

efa = Prepend[FinancialData["EFA", "Jan.1,2012"], {, "EFA"}];

Export["data.xls", {spy, efa}, "XLS"]
  • $\begingroup$ Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$ – bbgodfrey Feb 19 '15 at 5:10

If spy and efa have the same length, you can use Transpose:

Export["data.xls", Transpose[Join @@ Transpose /@ {spy, efa}]]

enter image description here

Update: If spy and efa have different lengths, you can use PadRight before the outermost Transpose:

d1 = FinancialData["SPY", "Jan.1,2012"];
d2 = FinancialData["EFA", "Jan.1,2012"];

spyb = Prepend[d1[[;; 10]], {, "SPY"}];
efab = Prepend[d2[[;; 20]], {, "EFA"}];

Export["data3.xls", Transpose[PadRight[Join @@ Transpose /@ {spyb, efab}, Automatic, ""]]]

enter image description here

  • $\begingroup$ Thanks, kguler. This is a simple fix. However, there will be times when spy, efa or others will not be the same length. Any suggestions for creating the same type of format if they have different lengths? $\endgroup$ – Anthony Parish Feb 20 '15 at 2:13
  • $\begingroup$ @Anthony, please see the update for an approach to deal with different-length columns. $\endgroup$ – kglr Feb 20 '15 at 6:17

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.