将GridView导入到Excel和word(完全可实现)
2007-05-21 20:35
507 查看
需要注意两个地方:
1.EnableEventValidation="false" 必须加
2.下面这个事件必须加
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
在我的资源里有相应的原代码下载
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="excel.aspx.cs" EnableEventValidation="false" Inherits="将GridView导出到Excel_excel" %>
<!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>将Gridview导出到Excel,Word</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="stuid" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="stuid" HeaderText="编号" InsertVisible="False" ReadOnly="True"
SortExpression="stuid" />
<asp:BoundField DataField="stuname" HeaderText="姓名" SortExpression="stuname" />
<asp:CheckBoxField DataField="stusex" HeaderText="性别" SortExpression="stusex" />
<asp:BoundField DataField="stuaddress" HeaderText="地址" SortExpression="stuaddress" />
<asp:BoundField DataField="stuage" HeaderText="年龄" SortExpression="stuage" />
<asp:BoundField DataField="stutuition" HeaderText="学费" SortExpression="stutuition" />
<asp:BoundField DataField="stuaveragescore" HeaderText="成绩" SortExpression="stuaveragescore" />
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myTestGridViewConnectionString %>"
SelectCommand="SELECT * FROM [stu]"></asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Text="导出到Excel" OnClick="Button1_Click" />
<br />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="导出到Word" Width="108px" />
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.IO;
public partial class 将GridView导出到Excel_excel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void ExportToExcel(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
//导出到Excel
protected void Button1_Click(object sender, EventArgs e)
{
ExportToExcel("application/ms-excel","学生信息.xls");
}
//导出到Word
protected void Button2_Click(object sender, EventArgs e)
{
ExportToExcel("application/ms-excel", "学生信息.doc");
//ExportToExcel("application/ms-word", "学生信息.doc");//都可以
}
//这个事件必须加
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
}
当然上面的这些东东也是我从CSDN上面学来的,有一篇是看的青青月儿的,还有一篇,忘了是谁的啦,反正挺感谢的。
1.EnableEventValidation="false" 必须加
2.下面这个事件必须加
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
在我的资源里有相应的原代码下载
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="excel.aspx.cs" EnableEventValidation="false" Inherits="将GridView导出到Excel_excel" %>
<!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>将Gridview导出到Excel,Word</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="stuid" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="stuid" HeaderText="编号" InsertVisible="False" ReadOnly="True"
SortExpression="stuid" />
<asp:BoundField DataField="stuname" HeaderText="姓名" SortExpression="stuname" />
<asp:CheckBoxField DataField="stusex" HeaderText="性别" SortExpression="stusex" />
<asp:BoundField DataField="stuaddress" HeaderText="地址" SortExpression="stuaddress" />
<asp:BoundField DataField="stuage" HeaderText="年龄" SortExpression="stuage" />
<asp:BoundField DataField="stutuition" HeaderText="学费" SortExpression="stutuition" />
<asp:BoundField DataField="stuaveragescore" HeaderText="成绩" SortExpression="stuaveragescore" />
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myTestGridViewConnectionString %>"
SelectCommand="SELECT * FROM [stu]"></asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Text="导出到Excel" OnClick="Button1_Click" />
<br />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="导出到Word" Width="108px" />
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.IO;
public partial class 将GridView导出到Excel_excel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void ExportToExcel(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
//导出到Excel
protected void Button1_Click(object sender, EventArgs e)
{
ExportToExcel("application/ms-excel","学生信息.xls");
}
//导出到Word
protected void Button2_Click(object sender, EventArgs e)
{
ExportToExcel("application/ms-excel", "学生信息.doc");
//ExportToExcel("application/ms-word", "学生信息.doc");//都可以
}
//这个事件必须加
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
}
当然上面的这些东东也是我从CSDN上面学来的,有一篇是看的青青月儿的,还有一篇,忘了是谁的啦,反正挺感谢的。
相关文章推荐
- 将GridView导入到Excel和word(完全可实现)
- 使用phpExcel实现Excel数据的导入导出(完全步骤)
- 使用NPOI实现word和excel的导入导出
- javascript客户端实现导入到excel、word
- 将GridView中内容导入到Excel(或Word)中
- javascript客户端实现导入到excel、word
- 在应用程序上实现OFFICE相关的WORD EXCEL导入导出操作
- 将GridView导入到Excel和word
- 怎么把多个GridView和Repeater导入到word或者excel中
- 使用phpExcel实现Excel数据的导入导出(完全步骤)
- asp.net中实现GridView中的数据导入到Excel中
- ThinkPHP使用phpExcel实现Excel数据的导入导出(真正的完全步骤)
- 使用NPOI实现word和excel的导入导出
- 使用phpExcel实现Excel数据的导入导出(完全步骤)
- 如何实现数据从 txt/word 自动导入到excel中
- 将GridView中内容导入到Excel(或Word)中
- 使用phpExcel实现Excel数据的导入导出(完全步骤)
- GridView导入到word Excel
- 手把手教你如何扩展GridView之自带Excel和Word导出
- JeeSite中实现导入导出Excel步骤