Tuesday, March 11, 2008

Export data from a web page to Excel

I have severel times tried to export data from a web page to Excel. previously I have tried different librarys, but non of them have worked very well. Yesterday I found a solution in Tim Mackey's Weblog. His code worked very vell, and I was able to export data from my web page in matter of secounds!

using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Whatever {
///
/// This class provides a method to write a dataset to the HttpResponse as
/// an excel file.
///
public class ExcelExport {
public static void ExportDataSetToExcel(DataSet ds, string filename) {
HttpResponse response = HttpContext.Current.Response;

// first let's clean up the response.object
response.Clear();
response.Charset = "";

// set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");

// create a string writer
using (StringWriter sw = new StringWriter()) {
using (HtmlTextWriter htw = new HtmlTextWriter(sw)) {
// instantiate a datagrid
DataGrid dg = new DataGrid();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}
}
}


You could of cource use a DataTable instead of the DataSet.

Tim's post is availible here