GridView导出到Excel操作
2015-10-19 15:59
330 查看
aspx.cs页面写一段代码:
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
aspx前端页面顶端写:EnableEventValidation = "false",例如:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AdminMain.aspx.cs" Inherits="QDSJ_Web.Admin.AdminMain" EnableEventValidation = "false" %>
点击【导出】按钮:
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
aspx前端页面顶端写:EnableEventValidation = "false",例如:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AdminMain.aspx.cs" Inherits="QDSJ_Web.Admin.AdminMain" EnableEventValidation = "false" %>
点击【导出】按钮:
protected void btnToExcel_Click(object sender, EventArgs e) { Response.Clear(); Response.BufferOutput = true; //设定输出的字符集 Response.Charset = "GB2312"; //假定导出的文件名为FileName.xls Response.AppendHeader("Content-Disposition", "attachment;filename=WebInfo.xls"); Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //设置导出文件的格式 Response.ContentType = "application/vnd.ms-excel"; //关闭ViewState EnableViewState = false; System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true); System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo); System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter); GridView1.RenderControl(textWriter); //把HTML写回浏览器 Response.Write(stringWriter.ToString()); Response.End(); }
相关文章推荐
- Windows上安装Jekyll
- Oracle TO_DATE 日期格式
- Java基础-----反射的理解
- 写了10年Javascript未必全了解的连续赋值运算
- @suppressWarnings("unchecked") java 中是什么意思
- C/C++与汇编混合编程简介
- eclipse操作技巧
- BP网络在MATLAB工具箱中常用的几类函数
- 简单通用JDBC辅助类封装
- [转载]Google Android开发精华教程
- 复合主键
- ASE team work proposal
- iOS 界面布局
- 基于MD的新浪微博 Sweetheart 5.4.0已推出,想开发新浪微博的朋友可以和我交流
- linux教程:[4]配置Tomcat开机启动
- oracle 11g sqlplus下无法切换中文输入法
- 在VS中向命令行添加参数的方法
- IGMPv1与IGMPv2在同一子网兼容性问题
- Qt学习之路(58): 进程间交互
- Linux 安装Telnet