# Export DataSet as CSV or XLS

One question:

How can I export a DataSet to CSV and XLS ?

Dataset[{<|"A" -> 1, "B" -> 2|>, <|"A" -> 3, "B" -> 4|>, <|"A" -> 5,
"B" -> 6|>}]

• Have a look at ExportString[dataset, "CSV"]. Commented Apr 10, 2018 at 15:27
• @J.M. I got the following error: "Dataset of the given form could not be exported as format \ \"\!$$\"CSV\"$$\"" Commented Apr 10, 2018 at 15:31
• Interesting... I can export that particular example in 11.2. Commented Apr 10, 2018 at 15:32
• @J.M. Maybe it is the version of Mathematica... I have 11.0 and I tried it with an even simple example (see question) and I still get the same error. Commented Apr 10, 2018 at 15:33
• You might see if this helps mathematica.stackexchange.com/questions/56737/… or you can search mathematica.stackexchange for Export Dataset and find a variety of questions and answers
– Bill
Commented Apr 10, 2018 at 16:11

## 2 Answers

You can do the following:

entries = List @@@ Normal@dataset;
keys = dataset[1, Keys] // Normal

csvDataSet = PrependTo[entries, keys];

Export["text.csv", csvDataSet]

• Nice ! Works ! :) Commented Apr 10, 2018 at 18:29
• I use a similar defined function frequently: assocToLists[assoc_] := Prepend[Values[Normal[assoc]], First[Keys[Normal[assoc]]]]; Commented Oct 14, 2018 at 0:28
• Any idea on how to answer the second part, export to XLS or XLSX? Commented Nov 14, 2023 at 16:12

According to documentation, Export can directly push out a dataset as an CSV. The below statements work fine.

ds = Dataset[{
<|"a" -> 1, "b" -> 2, "Lunch" -> "Chicken",
"Luncheon Options" -> "", "Lunch Choices" -> ""|>,
<|"a" -> 2, "b" -> 6, "Lunch" -> "", "Luncheon Options" -> "Beef",
"Lunch Choices" -> ""|>,
<|"a" -> 3, "b" -> "7", "Lunch" -> "", "Luncheon Options" -> "",
"Lunch Choices" -> "Lamb"|>
}]
Export["Downloads/Lunch Options.csv", ds]