在网上找的一段代码:
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
导出的excel文件是空的,不知道怎么回事,请高人指教啊
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
导出的excel文件是空的,不知道怎么回事,请高人指教啊
解决方案 »
- 【在线等答案,关于“未将对象引用设置到对象的实例。”】
- 取特定的字符串怎么搞
- 怎样在linux中Mono平台上的GtkSharp程序上实现Win下的GridView显示效果来显示Mysql中带图片的表
- 一个新问题, 有难度. 如何根据用户名密码自动登陆到adsl, 防火墙.
- 急,请各位帮忙
- 程序在后台如何监视键盘按键和鼠标移动?
- 在C#中为什么执行SQL的语句出错
- 不明白!HTML和WEB窗体都有image及其他相同的控件,问他们能实现相同的功能吗?有区别吗?
- ???? 托盘问题 ????
- 提议:请时常用ADO编写数据库程序的人留下联系方式,方便交流!
- .net VS2005 里实现水印功能的控件。
- 求C#中office2007样式的控件!
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;/// <summary>
/// GetExcel 的摘要说明
/// 功能说明:.net2.0页面上的gridview及DataList等导出到excel的通用类
/// 作者:梁晓江
/// </summary>
public class GetExcel : System.Web.UI.Page
{
public GetExcel()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public StringWriter Export(string FileName, GridView gv)
{
System.Web.HttpContext.Current.Response.Charset = "UTF-8";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
System.Web.HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");//
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
gv.RenderControl(hw);
return tw;
}
//传入值filetype可以传入 "application/ms-word"使用word格式;
public StringWriter Export(string FileType, string FileName,GridView gv)
{
System.Web.HttpContext.Current.Response.Charset = "UTF-8";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename="+ HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
System.Web.HttpContext.Current.Response.ContentType = FileType;
System.Web.HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");//
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
gv.RenderControl(hw);
return tw;
}
//重载函数,有表格的头部和尾部时调用
public StringWriter Export(string FileType, string FileName, string Tablebegin, string Tableend, GridView gv)
{
System.Web.HttpContext.Current.Response.Charset = "UTF-8";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
System.Web.HttpContext.Current.Response.ContentType = FileType;
System.Web.HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");//
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
//表头
if (!Tablebegin.Equals(""))
{
hw.WriteLine(Tablebegin);
}
//表内容
gv.RenderControl(hw);
//表尾部设置
if (!Tableend.Equals(""))
{
hw.WriteLine(Tableend);
}
return tw;
}
//datalist倒出excel
public StringWriter Export(string FileType, string FileName,DataList dl)
{
System.Web.HttpContext.Current.Response.Charset = "GB2312";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
System.Web.HttpContext.Current.Response.ContentType = FileType;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
dl.RenderControl(hw);
return tw;
}
public override void VerifyRenderingInServerForm(Control control)
{
}
}
/*使用说明
1、需要在使用的网页中添加一个方法,例如
public override void VerifyRenderingInServerForm(Control control)// 这个是必须的。放在aspx.cs中。否则报错
{ }
protected void Button2_Click(object sender, EventArgs e)//这个是导出按钮的代码
{
this.GridView1.AllowPaging = false;//适用于分页显示的页
Bind();//适用于分页显示的页
GetExcel ge = new GetExcel();
Response.Write(ge.Export("application/ms-excel", "xx.xls",this.GridView1));
Response.End();
this.GridView1.AllowPaging =true;//适用于分页显示
}
2、对于含有“编辑”“删除”等按钮的gridview。需要修改页面文件的头文件中page指令的属性EnableEventValidation="false"
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeFile="1stocktake.aspx.cs" Inherits="jycode_zhuanye_cstmyy" %>
3、自定义数据源的分页代码
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
Bind();
}
*/
然后全部复制-粘贴。直接用就行。数据超过excel一个sheet允许的最大行数不支持。excel2003测试通过。excel2007每测试。
excel2003版最多65536行,2007版有1048576行