zoukankan
html css js c++ java
个人学习代码保存:例12.读取GridView文件中的数据到Excel文件
前台代码:Default.aspx
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Default.aspx.cs
"
Inherits
=
"
_Default
"
EnableEventValidation
=
"
false
"
%>
<!--
EnableEventValidation = "false" 用GridView导出Execl的时候,会发生只能在执行 Render() 的过程中调用 RegisterForEventValidation的错误提示。
-->
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
>
无标题页
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
>
<
asp:GridView
ID
="GridView1"
runat
="server"
>
</
asp:GridView
>
</
div
>
<
asp:Button
ID
="Button1"
runat
="server"
OnClick
="Button1_Click"
Text
="导出Excel"
/>
</
form
>
</
body
>
</
html
>
后台代码:Default.aspx.cs
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Data.SqlClient;
public
partial
class
_Default : System.Web.UI.Page
{
private
static
string
connstr
=
ConfigurationManager.AppSettings[
"
ConnectionString
"
].ToString();
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(
!
IsPostBack)
{
this
.GridView1.DataSource
=
GetData();
this
.GridView1.DataBind();
}
}
public
DataSet GetData()
{
SqlConnection con
=
new
SqlConnection(connstr);
if
(con.State.Equals(ConnectionState.Closed))
{
con.Open();
}
string
sql
=
"
select * from guestbook
"
;
SqlCommand cmd
=
new
SqlCommand(sql,con);
SqlDataAdapter sda
=
new
SqlDataAdapter(cmd);
DataSet ds
=
new
DataSet();
sda.Fill(ds);
con.Close();
return
ds;
}
//
否则会出现:类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。
public
override
void
VerifyRenderingInServerForm(Control control)
{
//
Confirms that an HtmlForm control is rendered for
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
ExportDataGrid(
"
online/ms-excel
"
,
"
ddd.xls
"
);
}
private
void
ExportDataGrid(
string
FileType,
string
FileName)
{
Response.Clear();
Response.Buffer
=
true
;
Response.Charset
=
"
utf-7
"
;
Response.AppendHeader(
"
Content-Disposition
"
,
"
attachment;filename=FileFlow.xls
"
);
Response.ContentEncoding
=
System.Text.Encoding.GetEncoding(
"
utf-7
"
);
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
.GridView1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
}
查看全文
相关阅读:
注解之------@WebService
注解之----@component,@controller,@service ,@repository简介
Java 之 I/O
标签之----@Override
注解之----@Resource
弹指之间 -- 简谱
弹指之间 -- Prerequisites
MVC5 + EF6 完整入门教程三
MVC5 + EF6 入门完整教程二
MVC5 + EF6 入门完整教程
原文地址:https://www.cnblogs.com/wbcms/p/1037569.html
最新文章
T-SQL去除重复行
BI工具Board-Change webapi port
粤语拼音表
YouTube视频下载解析网站
IBM Cloud Speech to Text 语音识别
ASP.NET Web API 2 过滤器
ASP.NET Web API 2 消息处理管道
T-SQL 编程技巧
C# using 的用法
使用 Quartz.NET 实现作业串行执行
热门文章
Windows 常用快捷键
计算机的使用基础
SQL Server Management Studio 使用技巧
解决 Entity Framework 6.0 decimal 类型精度问题
Entity Framework 6.0 常见异常及解决办法
AJAX(转载)
Spring 和Spring MVC 总结(ssh和ssm比较)
Spring MVC 和 Spring 总结(ssh和ssm比较)
注解之------Spring注解汇总
注解之------@WebParam,@WebResult
Copyright © 2011-2022 走看看