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#中的打印问题? 唤醒线程后抛异常---100分相送 如何判斷某數據集中的某表中有沒有某一名的字段。急。 FLASH 和 .NET 组件服务器开发 请教 C#效率问题讨论(请大家帮忙) 我的内部版本号怎么不自动增加了? 如何结束excel进程 求助. 想定义一个时间变量,EventTime,设其初值为0:12:383,即0分12秒383毫秒,怎么定义?? 各位请进来看看~~ 微信公众平台获取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);
}