求一个在asp.net中将DataGrid中的数据导出到EXCEL中 求高手们给一个在asp.net中将DataGrid中的数据导出到EXCEL中,小弟不胜感激。最好不要那个一输入名字就弹出另存为的窗口的那个例子。那个效果一点都不好。连ASP。NET的布局都导出了!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你去codeproject里面搜索datagrid+excel 留下你的EMAIL地址,我回去后发段代码给你,很好用的,我现在在公司,公司里没有 [email protected]给我一份,谢谢! 一下代码演示点击按钮将datagrid的数据导出到excel,当然datagrid的数据就是数据库的private void Page_Load(object sender, System.EventArgs e){DataSet objDataset = new DataSet();SqlConnection objConn = new SqlConnection();objConn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());objConn.Open();SqlDataAdapter objAdapter = new SqlDataAdapter("Select top 5 * from customers where country='USA'",objConn);objAdapter.Fill(objDataset);DataView oView = new DataView(objDataset.Tables[0]);dgExcel.DataSource = oView;dgExcel.DataBind();objConn.Close();objConn.Dispose();objConn = null;if(Request.QueryString["bExcel"] == "1"){Response.ContentType = "application/vnd.ms-excel";Response.Charset = "";//关闭 ViewStateEnableViewState = false;System.IO.StringWriter tw = new System.IO.StringWriter();//将信息写入字符串System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);//在WEB窗体页上写出一系列连续的HTML特定字符和文本。//此类提供ASP.NET服务器控件在将HTML内容呈现给客户端时所使用的格式化功能//获取control的HTMLdgExcel.RenderControl(hw);//将DATAGRID中的内容输出到HtmlTextWriter对象中// 把HTML写回浏览器Response.Write(tw.ToString());Response.End();}}#region Web Form Designer generated codeoverride protected void OnInit(EventArgs e){//// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。//InitializeComponent();base.OnInit(e);}/// <summary>/// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。/// </summary>private void InitializeComponent(){ this.btnGetExcel.Click += new System.EventHandler(this.btnGetExcel_Click);this.Load += new System.EventHandler(this.Page_Load);}#endregionprivate void btnGetExcel_Click(object sender, System.EventArgs e){Response.Redirect("excel.aspx?bExcel=1");} 能给我发一份吗 [email protected]感谢 `` 感谢``` public static void ToExcel(System.Web.UI.Control ctl,string FileName) { HttpContext.Current.Response.Charset ="UTF-8"; HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default; HttpContext.Current.Response.ContentType ="application/ms-excel"; HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+""+FileName+".xls"); ctl.Page.EnableViewState =false; System.IO.StringWriter tw = new System.IO.StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); ctl.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End(); } 晕你们发的不都是点击按钮。然后就弹出一个保存的对话框。然后就保存。但你们有没有试过用SQL管理器导入刚刚保存的那个XLS,看会出现什么问题 HttpContext.Current.Response.ContentType ="application/ms-excel";还有一个问题。这条语句后面能不能指定类型的。比如是97-2000,5.0之类的类型。 C# 怎么读取网站地址栏图标? ◆执行另一个窗体的JavaScript函数◆ 请问CSDN论坛“载入中……”这种效果怎么实现? 我有一框架网........... datetime加毫秒的问题 有url重写需求的去下载代码吧 如何获得画图程序中出现的图像坐标呢 QQ警告框的特殊效果是怎么实现的 为什么我的VS.net 2003中找不到treeview控件? 关于数据库的问题 已达到系统管理员配置的最大报表处理作业数限制。 如何弹出一个不能被IE阻止的窗体?
给我一份,谢谢!
{
DataSet objDataset = new DataSet();
SqlConnection objConn = new SqlConnection();
objConn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
objConn.Open();
SqlDataAdapter objAdapter = new SqlDataAdapter("Select top 5 * from customers where country='USA'",objConn);
objAdapter.Fill(objDataset);
DataView oView = new DataView(objDataset.Tables[0]);
dgExcel.DataSource = oView;
dgExcel.DataBind();
objConn.Close();
objConn.Dispose();
objConn = null;
if(Request.QueryString["bExcel"] == "1")
{
Response.ContentType = "application/vnd.ms-excel";Response.Charset = "";//关闭 ViewState
EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();//将信息写入字符串
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);//在WEB窗体页上写出一系列连续的HTML特定字符和文本。
//此类提供ASP.NET服务器控件在将HTML内容呈现给客户端时所使用的格式化功能
//获取control的HTML
dgExcel.RenderControl(hw);//将DATAGRID中的内容输出到HtmlTextWriter对象中
// 把HTML写回浏览器
Response.Write(tw.ToString());
Response.End();
}}#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnGetExcel.Click += new System.EventHandler(this.btnGetExcel_Click);
this.Load += new System.EventHandler(this.Page_Load);}
#endregionprivate void btnGetExcel_Click(object sender, System.EventArgs e)
{
Response.Redirect("excel.aspx?bExcel=1");
}
[email protected]
感谢 `` 感谢```
{
HttpContext.Current.Response.Charset ="UTF-8";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+""+FileName+".xls");
ctl.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
还有一个问题。这条语句后面能不能指定类型的。
比如是97-2000,5.0之类的类型。