您的位置:首页 > 其它

导出查询数据生成EXCEL文件并下载

2010-04-26 17:12 525 查看
/// <summary>
/// 生成EXCEL
/// </summary>
public void ToExcel(System.Web.UI.WebControls.DataGrid DataGrid2Excel, string FileName, string Title, string Head)
{
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
FrontDecorator(hw);
if (Title != "")
hw.Write(Title + "<br>");
if (Head != "")
hw.Write(Head + "<br>");
DataGrid2Excel.EnableViewState = false;
DataGrid2Excel.RenderControl(hw);
RearDecorator(hw);
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
response.Buffer = true;
response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
response.ContentType = "application/vnd.ms-excel";
response.Write("<meta   http-equiv=Content-Type   content=text/html;charset=GB2312>");
string file = HttpUtility.UrlEncode(FileName + ".xls", System.Text.Encoding.GetEncoding("GB2312"));
response.AddHeader("Content-Disposition", "attachment; filename=" + file);
response.Charset = "GB2312";
response.Write(sw.ToString());
response.End();
}
private static void RearDecorator(HtmlTextWriter writer)
{
writer.WriteEndTag("Body");
writer.WriteEndTag("HTML");
}
//入口
public void ToExcel(DataTable dt, string FileName)
{
System.Web.UI.WebControls.DataGrid dgTemp = new System.Web.UI.WebControls.DataGrid();
dgTemp.DataSource = dt;
dgTemp.DataBind();
ToExcel(dgTemp, FileName, "", "");
}
//FrontDecorator方法
private static void FrontDecorator(HtmlTextWriter writer)
{
writer.WriteFullBeginTag("HTML");
writer.WriteFullBeginTag("Head");
writer.WriteEndTag("Head");
writer.WriteFullBeginTag("Body");
}
//// <summary>
/// 导出Excel
/// </summary>
/// <param name="dt">表</param>
public static void InitExcel(DataTable dtt)
{
DataGrid dgExport = null;
StringWriter strWriter = null;
HtmlTextWriter htmlWriter = null;
System.Data.DataTable dt = dtt;
if (dt != null)
{
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.Write("<meta   http-equiv=Content-Type   content=text/html;charset=GB2312>");
HttpContext.Current.Response.Charset = "";
strWriter = new StringWriter();
htmlWriter = new HtmlTextWriter(strWriter);
dgExport = new DataGrid();
dgExport.DataSource = dt.DefaultView;
dgExport.AllowPaging = false;
dgExport.HeaderStyle.ForeColor = System.Drawing.Color.Blue;
dgExport.HeaderStyle.BackColor = System.Drawing.Color.Red;
dgExport.DataBind();
dgExport.RenderControl(htmlWriter);
HttpContext.Current.Response.Write(strWriter.ToString());
HttpContext.Current.Response.End();
}
}

protected void Button1_Click(object sender, EventArgs e)
{
//CMS.BLL.t_user_infoM userinfo = new CMS.BLL.t_user_infoM();
//IDataReader ida = userinfo.GetExeCmdResult("[dbo].[sp_t_elite_week_return_check3] @game_id=1,@sta_id=1,@order_expression='wrt.sta_taxis'");
CMS.BLL.NewsManage nm = new CMS.BLL.NewsManage();
IDataReader ida = nm.GetExeCmdResult("select * from cms_newst");
DataTable dt = CMS.CommonUtility.BXDBUtility.DataReaderToDataTable(ida);
this.ToExcel(dt,"aaa");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐