62,046
社区成员
发帖
与我相关
我的任务
分享
//设定导出文件的格式
Response.ContentType = "application/vnd.ms-excel";
//设定编码方式
Response.Charset = "gb2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
//关闭ViewState
EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
this.dq_GridView.RenderControl(hw);
//把HTML写回浏览器
Response.Write(tw.ToString());
Response.End();
<meta http-equiv="content-type" content="application/ms-word; charset=UTF-8"/>
//如果没有下面方法会报错类型“GridView”的控件“GridView1”
//必须放在具有 runat=server 的窗体标记内
//导出到Excel用
public override void VerifyRenderingInServerForm(Control control)
{
}
/// <summary>
/// 导出数据到Excel
/// </summary>
public static void Export(Page pg, Repeater rep, string FileName)
{
pg.Response.Clear();
pg.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
pg.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
pg.Response.ContentEncoding = System.Text.Encoding.UTF8;
pg.Response.ContentType = "application/vnd.ms-excel";//设置输出文件类型为excel文件。
pg.Response.Charset = "";
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
rep.RenderControl(oHtmlTextWriter);
pg.Response.Output.Write(oStringWriter.ToString());
pg.Response.End();
}