GridView如阿生成Excel或者word数据
GridView如阿生成Excel(word)数据:
(一)页面部分代码显示如下:
1<body>
2 <form id="form1" runat="server">
3 <div>
4 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
5
6 </div>
7 <asp:GridView ID="GridView1" runat="server">
8 </asp:GridView>
9 </form>
10</body>
2 <form id="form1" runat="server">
3 <div>
4 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
5
6 </div>
7 <asp:GridView ID="GridView1" runat="server">
8 </asp:GridView>
9 </form>
10</body>
(二)后台代码如下:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.IO; //导入文件
12using System.Data.SqlClient; //导入文件
13public partial class Default2 : System.Web.UI.Page
14{
15 public DataTable dt = null;
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 if (!Page.IsPostBack)
19 {
20 Bind();
21 }
22 }
23 protected void Bind()
24 {
25 string strSql = "select * from News";
26 using (SqlConnection conn = new SqlConnection("server=.;database=HCXD;user id=sa;password=sa;pooling=true;"))
27 using (SqlDataAdapter da = new SqlDataAdapter(strSql, conn))
28 {
29 try
30 {
31 conn.Open();
32 dt = new DataTable();
33 da.Fill(dt);
34 this.GridView1.DataSource = dt;
35 this.GridView1.DataBind();
36 }
37 catch(Exception err)
38 {
39 throw new Exception("错误信息如下:"+err.Message.ToString());
40 }
41 }
42 }
43
44 public override void VerifyRenderingInServerForm(Control control)
45 {
46 //base.VerifyRenderingInServerForm(control);
47 }
48
49 protected void Button1_Click(object sender, EventArgs e)
50 {
51 //清除客户端当前显示
52 Response.Clear();
53 //作为附件输出,filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc .xls .txt .htm
54 Response.AddHeader("content-disposition", "attachment;filename=FileName.xls"); //显示标头
55 //设置显示的字和内容要存的形式
56 Response.Charset = "gb2312";
57 Response.ContentType = "application/vnd.xls";
58 System.IO.StringWriter stringWrite = new System.IO.StringWriter();
59 System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
60 GridView1.AllowPaging = false;
61 Bind();
62 GridView1.RenderControl(htmlWrite);
63 Response.Write(stringWrite.ToString());
64 Response.End();
65 GridView1.AllowPaging = true;
66 Bind();
67 }
68}
69
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.IO; //导入文件
12using System.Data.SqlClient; //导入文件
13public partial class Default2 : System.Web.UI.Page
14{
15 public DataTable dt = null;
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 if (!Page.IsPostBack)
19 {
20 Bind();
21 }
22 }
23 protected void Bind()
24 {
25 string strSql = "select * from News";
26 using (SqlConnection conn = new SqlConnection("server=.;database=HCXD;user id=sa;password=sa;pooling=true;"))
27 using (SqlDataAdapter da = new SqlDataAdapter(strSql, conn))
28 {
29 try
30 {
31 conn.Open();
32 dt = new DataTable();
33 da.Fill(dt);
34 this.GridView1.DataSource = dt;
35 this.GridView1.DataBind();
36 }
37 catch(Exception err)
38 {
39 throw new Exception("错误信息如下:"+err.Message.ToString());
40 }
41 }
42 }
43
44 public override void VerifyRenderingInServerForm(Control control)
45 {
46 //base.VerifyRenderingInServerForm(control);
47 }
48
49 protected void Button1_Click(object sender, EventArgs e)
50 {
51 //清除客户端当前显示
52 Response.Clear();
53 //作为附件输出,filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc .xls .txt .htm
54 Response.AddHeader("content-disposition", "attachment;filename=FileName.xls"); //显示标头
55 //设置显示的字和内容要存的形式
56 Response.Charset = "gb2312";
57 Response.ContentType = "application/vnd.xls";
58 System.IO.StringWriter stringWrite = new System.IO.StringWriter();
59 System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
60 GridView1.AllowPaging = false;
61 Bind();
62 GridView1.RenderControl(htmlWrite);
63 Response.Write(stringWrite.ToString());
64 Response.End();
65 GridView1.AllowPaging = true;
66 Bind();
67 }
68}
69
注意:
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
这段代码必须写上,否则报{
//base.VerifyRenderingInServerForm(control);
}