Export dataset to CSV file using C#

Create a dataset

Example:
string strReferral = //your query

DataSet dsPe = SqlHelper.ExecuteDataset(new Connection().GetCon, CommandType.Text, strReferral);

//pass the dataset as argument to this function
ExportDataSet(dsPe);

public void ExportDataSet(DataSet ds)
{
string filename = "MyExcelFile.xls";
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
}

Comments

Post a Comment