All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
SQL Server 索引基础知识(3)----测试中一些常看的指标和清除缓存的方法_[SQL Server教程]
一步步教你配置SQL SERVER合并复制(四)设置Distributor的安全_[SQL Server教程]
Asp.net动态生成页面控件的办法_[Asp.Net教程]
《Effective,C#》:区别四个判等函数_.net资料_编程技术
SQL Server 2000 标准版安装_[SQL Server教程]
SQL2000系统表的应用_[SQL,Server教程]
弹出窗口window.open()的参数列表_JavaScript技术_编程技术
Asp.Net,通用数据操作类,(附通用数据基类)_[Asp.Net教程]
ASP.NET常用JS脚本整理_[Asp.Net教程]
sqlserver无法安装_[SQL,Server教程]
Reading number is top 10 pictures
Exquisite decoration is not paying too much3
Sora aoi be huged like a bear
The money of more than 100 countries and regions9
BingBingFan apple dew point photo gallery4
10 powerless things in life
Seductive beauty of crime1
梦幻的风景
In the world the most mysterious 21 place landscape1
这年头,找个靠谱的妹子太难了
China's zhejiang university to create the world's most light material
Download software ranking
Tram sex maniac 2 (H) rar bag2
Boxer vs Yellow5
The cock of the Grosvenor LTD handsome
终极变速大师Speeder3.26
Ashlynn Video3
Desire a peach blossom
c#程序设计案例教程
打鸟视频
双旗镇刀客A
SQL2000 For 4IN1
delv published in(发表于) 2014/1/27 6:46:09 Edit(编辑)
Asp.net,2.0,GridView数据导出Excel文件(示例代码下载)_[Asp.Net教程]

Asp.net,2.0,GridView数据导出Excel文件(示例代码下载)_[Asp.Net教程]

Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)_[Asp.Net教程]

Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)


(一) . 运行示例图


1. 待导出数据的GridView图:



2. 生成的Excel文件



设计家园 收集整理


(二). 代码


1. 前台页面 GridViewToExcelFile.aspx 代码:


1<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeFile="GridViewToExcelFile.aspx.cs" Inherits="_Default" %>
2
3
4
5
6
7 Untitled Page
8
9
10
11

12
13
14
15
16
17
18

19

20 onClick="Button1_Click"
21 Text="Create Excel" Width="137px" />
22

23
24
25


2. 页面后台文件 GridViewToExcelFile.aspx.cs 代码:


1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.IO;
11 using System.Text;
12 using System.Collections;
13
14 public partial class _Default : System.Web.UI.Page
15 {
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 if(!Page.IsPostBack)
19 {
20 CreateStructure();
21 this.gvFoods.DataSource = this.CreateData();
22 this.gvFoods.DataBind();
23 }
24 }
25 private DataTable CreateStructure()
26 {
27 DataTable dt = new DataTable();
28 dt.Columns.Add(new DataColumn("CategoryID", typeof(int)));
29 dt.Columns.Add(new DataColumn("CategoryName", typeof(string)));
30 dt.Columns.Add(new DataColumn("Price", typeof(int)));
31 return dt;
32 }
33 public DataSet CreateData()
34 {
35 DataSet ds = new DataSet();
36 DataTable dt = this.CreateStructure();
37
38 DataRow drNew = dt.NewRow();
39 drNew = dt.NewRow();
40 drNew["CategoryID"] = 1;
41 drNew["CategoryName"] = "Apple";
42 drNew["Price"] = 2;
43 dt.Rows.Add(drNew);
44
45 drNew = dt.NewRow();
46 drNew["CategoryID"] = 2;
47 drNew["CategoryName"] = "Banana";
48 drNew["Price"] = 3;
49 dt.Rows.Add(drNew);
50
51 drNew = dt.NewRow();
52 drNew["CategoryID"] = 3;
53 drNew["CategoryName"] = "Orange";
54 drNew["Price"] = 1;
55 dt.Rows.Add(drNew);
56
57 drNew = dt.NewRow();
58 drNew["CategoryID"] = 4;
59 drNew["CategoryName"] = "Radish";
60 drNew["Price"] = 2;
61 dt.Rows.Add(drNew);
62
63 drNew = dt.NewRow();
64 drNew["CategoryID"] = 5;
65 drNew["CategoryName"] = "Pen";
66 drNew["Price"] = 3;
67 dt.Rows.Add(drNew);
68
69 drNew = dt.NewRow();
70 drNew["CategoryID"] = 6;
71 drNew["CategoryName"] = "Pencil";
72 drNew["Price"] = 7;
73 dt.Rows.Add(drNew);
74
75 drNew = dt.NewRow();
76 drNew["CategoryID"] = 7;
77 drNew["CategoryName"] = "Ruler";
78 drNew["Price"] = 3;
79 dt.Rows.Add(drNew);
80
81 drNew = dt.NewRow();
82 drNew["CategoryID"] = 8;
83 drNew["CategoryName"] = "Eraser";
84 drNew["Price"] = 5;
85 dt.Rows.Add(drNew);
86
87 ds.Tables.Add( dt );
88 return ds;
89 }
90 protected void Button1_Click(object sender, EventArgs e)
91 {
92 StringWriter sw = new StringWriter();
93 HtmlTextWriter htw = new HtmlTextWriter(sw);
94 this.gvFoods.RenderControl(htw);
95 string strHtml = sw.ToString().Trim();
96
97 string ExcelFileName = "FoodList.xls";
98 string FilePhysicialPathName = Request.PhysicalApplicationPath;
99
100 //生成的Excel文件名
101 string objectExcelFileName = Path.Combine(FilePhysicialPathName, ExcelFileName);
102
103 if( File.Exists( objectExcelFileName ))
104 {
105 File.Delete(objectExcelFileName);
106 }
107 FileStream fs = new FileStream(objectExcelFileName, FileMode.Create);
108 BinaryWriter bw = new BinaryWriter(fs, Encoding.GetEncoding("GB18030"));
109 bw.Write(strHtml);
110 bw.Close();
111 fs.Close();
112
113 }
114 public override void VerifyRenderingInServerForm(Control control)
115 {
116 //base.VerifyRenderingInServerForm(control);
117 }
118 }
119


(三). 示例代码下载


http://www.cnblogs.com/Files/ChengKing/GridViewToExcel.rar


设计家园 收集整理







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.