从Gridview 中导出数据到EXCEL
2013-05-14 22:59
316 查看
前端:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="false" %>
<asp:GridView ID="gv_Score" runat="server" CellPadding="4" ForeColor="#333333" AllowPaging="false">
再定义一个按钮,实现导出功能。
后台CS:
using System.IO;
protected void btn_Export_Click(object sender, EventArgs e)
{
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.AddHeader("Content-Disposition","attachment;FileName="+HttpUtility.UrlEncode(DateTime.Now.ToString(),System.Text.Encoding.UTF8)+".xls");
Response.ContentType = "application/ms-excel"; //输出文件类型为excel类型
this.EnableViewState = false;
StringWriter wt = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(wt);
gv_Score.RenderControl(hw);
Response.Write(wt.ToString());
Response.End();
}
//必须覆写些方法,不然会报错。
public override void VerifyRenderingInServerForm(Control control)
{
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="false" %>
<asp:GridView ID="gv_Score" runat="server" CellPadding="4" ForeColor="#333333" AllowPaging="false">
再定义一个按钮,实现导出功能。
后台CS:
using System.IO;
protected void btn_Export_Click(object sender, EventArgs e)
{
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.AddHeader("Content-Disposition","attachment;FileName="+HttpUtility.UrlEncode(DateTime.Now.ToString(),System.Text.Encoding.UTF8)+".xls");
Response.ContentType = "application/ms-excel"; //输出文件类型为excel类型
this.EnableViewState = false;
StringWriter wt = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(wt);
gv_Score.RenderControl(hw);
Response.Write(wt.ToString());
Response.End();
}
//必须覆写些方法,不然会报错。
public override void VerifyRenderingInServerForm(Control control)
{
}
相关文章推荐
- 将GridView中的数据导出到Excel中下载并且解决乱码的问题
- 在ASP.NET中将GridView数据导出到Word、Excel
- 将gridview中的数据导出到excel中
- asp.net 导出 gridview 数据 excel 全部 当前页 选择行 选中行 所选行 复选框
- ASP.NET GridView导出数据到Excel
- GridView中的数据导出为Excel
- Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)
- [导入]Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)
- 将dev gridview中数据原样导出到EXCEL
- GridView中的数据导出为Excel【转】,和以前的有变化
- Gridview中的数据导出到excel中
- 导出 Gridview数据到 Excel
- 导出GridView中的数据到Excel
- 不需要安装office excel,导出GridView所有数据为Excel
- 从gridview 中查询数据导出Excel 简单方法
- 【asp.net】将GridView数据导出Excel
- .net从gridview控件导出数据到excel续
- GridView导出数据到excel数字变为科学记数法的问题的解决办法
- GridView数据导出到Excel
- 将gridview 的数据导出EXCEL