Cybercrux

Everything is achievable through technology

Excel export

public void DataToExcel(DataTable dt, System.Web.HttpResponse CurrentResponse, string ExcelFileName)
{
	CurrentResponse.Clear();
	CurrentResponse.Charset = "";
	CurrentResponse.ContentType = "application/vnd.ms-excel";
	CurrentResponse.AddHeader("Content-Disposition", "attachment;filename=" + ExcelFileName + ".xls");
 
	//Here we set the correct encoding so that all characters show!
	CurrentResponse.ContentEncoding = System.Text.Encoding.UTF8;
	CurrentResponse.Charset = "65001";
	byte[] b = new byte[] {
		0xef,
		0xbb,
		0xbf
	};
	CurrentResponse.BinaryWrite(b);
 
	System.IO.StringWriter stringWrite = new System.IO.StringWriter();
	System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
	System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
	dg.DataSource = dt;
	dg.DataBind();
	dg.RenderControl(htmlWrite);
	CurrentResponse.Write(stringWrite.ToString);
	CurrentResponse.End();
}
Advertisements