急求!怎样通过button将DataGrid中的内容导出到Excel中????

loveqiudexiaochouyu 2010-05-29 08:36:14
怎样通过button将DataGrid中的内容导出到Excel中????最好是把源代码告诉我,谢谢各位了!!!请说得详细点,谢谢谢!!!
...全文
46 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
b87936260 2010-05-29
  • 打赏
  • 举报
回复
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["KTV"].ConnectionString);
conn.Open();
string sql = string.Format("SELECT * FROM MUSIC");
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "MUSIC");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "gb2312";
EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
}
b87936260 2010-05-29
  • 打赏
  • 举报
回复
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["KTV"].ConnectionString);
conn.Open();
string sql = string.Format("SELECT * FROM MUSIC");
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "MUSIC");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "gb2312";
EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
}
  • 打赏
  • 举报
回复
我说的是用C#编写的 程序
qq153325250 2010-05-29
  • 打赏
  • 举报
回复
学习学习学习学习学习学习
wuyq11 2010-05-29
  • 打赏
  • 举报
回复
Response.Clear();
Response.Buffer= false;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=test.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312"); Response.ContentType = "application/ms-excel"; this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧