日期:2014-05-19  浏览次数:20511 次

请问各位大虾~~DataGrid内容导出到excel 的问题
html:

    <asp:DataGrid   id= "sstu_scrore "   runat= "server "   Width= "640px "                           AutoGenerateColumns= "False "   Height= "96px ">
<Columns>

<asp:BoundColumn   DataField= "cno "   HeaderText= "课程号 "> </asp:BoundColumn>

<asp:BoundColumn   DataField= "cname "   HeaderText= "课程称 "> </asp:BoundColumn>
<asp:BoundColumn   DataField= "sno "   HeaderText= "学生学     号 ">   </asp:BoundColumn>
<asp:BoundColumn   DataField= "score "   HeaderText= "成绩 "> </asp:BoundColumn>
</Columns>

</asp:DataGrid>

请问应该如何利用一个   button   激发将查询到的DataGrid内容导出到到excel

在线等~~~先谢啦~~~     :)

------解决方案--------------------
教你一个方法
保存DataGrid到Excel实际就是保存你的DataGrid的DataSource(DataTable)到Excel
网上DataSet ,DataTable保存到Excel的例子很多,找不到我帮你找,在原来项目里有
------解决方案--------------------
http://dotnet.aspx.cc/article/8a4cbf47-b888-4832-3389-ed3a3a3c8aab/read.aspx
------解决方案--------------------
private void Button1_Click(object sender, System.EventArgs e)
{
//this.DataGrid1.AllowPaging = false;
//DB(); 这里需要加你绑定dridgrid的方法
Response.ContentType = "application/vnd.ms-excel ";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
DataGrid1.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();
}