download excel file from datagrid for webpage

/// <summary>
/// Summary description for WebExcelUtil.
/// </summary>
public sealed class WebExcelUtil
{
public static void GenerateExcel(DataSet objDS, HttpResponse objResponse)
{
if (objDS==null) return;
objResponse.Clear();
objResponse.Buffer = false;
objResponse.Charset = "GB2312";
objResponse.AppendHeader("Content-Disposition", "attachment;filename=Result.xls");
objResponse.ContentEncoding = Encoding.GetEncoding("GB2312");
objResponse.ContentType = "application/vnd.ms-excel";
objResponse.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");

StringWriter objStringWrite = new StringWriter();
HtmlTextWriter objHtmlWriter = new HtmlTextWriter(objStringWrite);
DataGrid objDataGrid = new DataGrid();

objDataGrid.DataSource = objDS.Tables[0];
objDataGrid.DataBind();
objDataGrid.RenderControl(objHtmlWriter);
objResponse.Write(objStringWrite.ToString());
objResponse.End();
}

public static void GenerateExcel(DataGrid objDataGrid, HttpResponse objResponse)
{
objResponse.Clear();
objResponse.Buffer = false;
objResponse.Charset = "GB2312";
objResponse.AppendHeader("Content-Disposition", "attachment;filename=Result.xls");
objResponse.ContentEncoding = Encoding.GetEncoding("GB2312");
objResponse.ContentType = "application/vnd.ms-excel";
objResponse.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");

StringWriter objStringWrite = new StringWriter();
HtmlTextWriter objHtmlWriter = new HtmlTextWriter(objStringWrite);

objDataGrid.RenderControl(objHtmlWriter);
objResponse.Write(objStringWrite.ToString());
objResponse.End();
}
}

你可能感兴趣的:(datagrid)