The following method would exactly suffice your requirement.
As I faced the same issue and happen to write the following code to get what I needed.
This converts the DataSet to XML and then to text/csv as you can see. There are few other ContentTypes that you may use to get what's required.
XML File:
<NewTable>
<Table>
<a1>12345</a1>
<a2>12345</a2>
<a3>12345</a3>
<a4>12345</a4>
<a5>12345</a5>
</Table>
</NewTable>
Function:
protected string ConvertToCSV(DataSet objDataSet)
{
string xmlInput = objDataSet.GetXml();
string csvOut = string.Empty;
XDocument doc = XDocument.Parse(xmlInput);
StringBuilder sb = new StringBuilder(100000);
foreach (XElement node in doc.Descendants("Table"))
{
foreach (XElement innerNode in node.Elements())
{
sb.AppendFormat("{0},", innerNode.Value);
}
sb.Remove(sb.Length - 1, 1);
sb.AppendLine();
}
csvOut = sb.ToString();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "text/csv";
Response.ContentEncoding = Encoding.GetEncoding(0);
Response.AppendHeader("Content-Disposition", "attachment;filename=Report.csv");
Response.Output.Write(csvOut);
Response.End();
return csvOut;
}