C#导出gridview到excel gridviewexcel导出c# 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 // 保存错误信息 GridView gv = new GridView(); gv.DataSource = dtError; gv.DataBind(); gv.Attributes.Add("style", "vnd.ms-excel.numberformat:@"); HttpResponse hResponse = this.Response; string fileName1 = "新员工格式验证错误统计" + DateTime.Now.ToString("yyyyMMdd"); hResponse.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName1, System.Text.Encoding.UTF8) + ".xls"); hResponse.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); hResponse.ContentType = "application/ms-excel"; this.EnableViewState = false; StringWriter tw = new StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); gv.RenderControl(hw); hResponse.Write(tw); hResponse.End();测试一下。 to:duanzi_peng测试了一下,这个在单独页面上是没有问题,可是我把你的代码放到 iframe的页面中,就又是保存整个页面了 protected void btnSaveExcel_Click(object sender, EventArgs e) { string FileName = "xxx"; System.IO.StringWriter objSW = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter objHTW = new System.Web.UI.HtmlTextWriter(objSW); try { //設定格式 Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", FileName)); Response.ContentType = "application/ms-excel";// "application/vnd.ms-excel"; Response.Charset = "UTF-8"; this.EnableViewState = false; this.GridViewData.RenderControl(objHTW); Response.Write(objSW.ToString()); Response.End(); } catch (Exception ex) { string aa = ex.Message.ToString(); } finally { objSW.Close(); objSW = null; objHTW.Close(); objHTW = null; } }///必须重载VerifyRenderingInServerForm, 不然会报错 public override void VerifyRenderingInServerForm(Control control) { //base.VerifyRenderingInServerForm(control); } 百度搜索下myxls吧 或者npoi 都可以.不建议用这样的方式下载... C#程序经常崩溃中断的处理方法讨论 获取异常 存储过程问题.C#调用存储返回多个参数 请问:如何在Internet实现QQ或MSN上点对点文件传输功能?在线等待... C#初学着,这么看类中的公有方法? 我出100分,请各位把C#比VB.net功能不爽的地方写出来 很困扰于一个有关treeview的问题 C#中日期相减后结果如何变成整数 system.windows.forms.dll 出错?? Convert.ToInt32("3")返回的怎么是0? 微信公众平台获取JSON的解析 【求助】c#工程灵异事件
// 保存错误信息
GridView gv = new GridView();
gv.DataSource = dtError;
gv.DataBind();
gv.Attributes.Add("style", "vnd.ms-excel.numberformat:@"); HttpResponse hResponse = this.Response;
string fileName1 = "新员工格式验证错误统计" + DateTime.Now.ToString("yyyyMMdd"); hResponse.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName1, System.Text.Encoding.UTF8) + ".xls");
hResponse.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
hResponse.ContentType = "application/ms-excel";
this.EnableViewState = false; StringWriter tw = new StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
gv.RenderControl(hw); hResponse.Write(tw);
hResponse.End();测试一下。
测试了一下,这个在单独页面上是没有问题,可是我把你的代码放到 iframe的页面中,就又是保存整个页面了
{
string FileName = "xxx";
System.IO.StringWriter objSW = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter objHTW = new System.Web.UI.HtmlTextWriter(objSW);
try
{
//設定格式
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", FileName));
Response.ContentType = "application/ms-excel";// "application/vnd.ms-excel";
Response.Charset = "UTF-8";
this.EnableViewState = false; this.GridViewData.RenderControl(objHTW);
Response.Write(objSW.ToString());
Response.End();
}
catch (Exception ex)
{
string aa = ex.Message.ToString();
}
finally
{
objSW.Close();
objSW = null;
objHTW.Close();
objHTW = null;
}
}
///必须重载VerifyRenderingInServerForm, 不然会报错
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}