1.在搞项目的时候一般会遇到,将GridView或者Repeater的内容以Excel的形式保存到本地,即导出功能。我总结了两个方法。
方法一:
1 DataTable dt = query.GetItems().GetDataTable();
2 if (dt != null)
3 {
4 HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=kehuziliao.xls");
5 HttpContext.Current.Response.Charset = "UTF-8";
6 HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
7 HttpContext.Current.Response.ContentType = "application/m-excel";
8 StringWriter sw = new StringWriter();
9 HtmlTextWriter htw = new HtmlTextWriter(sw);
10 GridView gdv = new GridView();
11 gdv.AllowPaging = false;
12 gdv.DataSource = dt;
13 gdv.DataBind();
14 gdv.RenderControl(htw);
15 HttpContext.Current.Response.Write(sw.ToString());
16 HttpContext.Current.Response.End();
17 }
2 if (dt != null)
3 {
4 HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=kehuziliao.xls");
5 HttpContext.Current.Response.Charset = "UTF-8";
6 HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
7 HttpContext.Current.Response.ContentType = "application/m-excel";
8 StringWriter sw = new StringWriter();
9 HtmlTextWriter htw = new HtmlTextWriter(sw);
10 GridView gdv = new GridView();
11 gdv.AllowPaging = false;
12 gdv.DataSource = dt;
13 gdv.DataBind();
14 gdv.RenderControl(htw);
15 HttpContext.Current.Response.Write(sw.ToString());
16 HttpContext.Current.Response.End();
17 }
方法二:
1 System.Data.DataTable dt = null;
2 dt = query.GetItems().GetDataTable();
3 System.Web.HttpContext curContext = System.Web.HttpContext.Current;
4 // 设置编码和附件格式
5 curContext.Response.ContentType = "application/vnd.ms-excel";
6 curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
7 curContext.Response.Charset = "utf-8";
8
9 // IO用于导出并返回excel文
10 StringWriter strWriter = new System.IO.StringWriter();
11 HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
12 GridView gvExport = new GridView();
13 gvExport.DataSource = dt.DefaultView;
14 gvExport.AllowPaging = false;
15 gvExport.DataBind();
16
17 // 返回客户端
18 gvExport.RenderControl(htmlWriter);
19 curContext.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />" + strWriter.ToString());
20 curContext.Response.End();
2 dt = query.GetItems().GetDataTable();
3 System.Web.HttpContext curContext = System.Web.HttpContext.Current;
4 // 设置编码和附件格式
5 curContext.Response.ContentType = "application/vnd.ms-excel";
6 curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
7 curContext.Response.Charset = "utf-8";
8
9 // IO用于导出并返回excel文
10 StringWriter strWriter = new System.IO.StringWriter();
11 HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
12 GridView gvExport = new GridView();
13 gvExport.DataSource = dt.DefaultView;
14 gvExport.AllowPaging = false;
15 gvExport.DataBind();
16
17 // 返回客户端
18 gvExport.RenderControl(htmlWriter);
19 curContext.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />" + strWriter.ToString());
20 curContext.Response.End();
本人也是 在 一头 雾水的情况下在网上找到的思路,并且加以理解,实现并且成功的。但是在生成的Excel后,用2007打开的时候提示如下:
这个问题还在研究中,希望知道的朋友不要小气,请告诉我,在这里先谢过浏览的朋友!谢谢!