# Export Multiple Arrays Into a Single Worksheet with Column Headers

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"]

• 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. – bbgodfrey Feb 19 '15 at 5:10

## 1 Answer

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

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


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, ""]]]


• 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? – Anthony Parish Feb 20 '15 at 2:13
• @Anthony, please see the update for an approach to deal with different-length columns. – kglr Feb 20 '15 at 6:17