将 DataGrid 的数据保存为 Excel 文件(孟子E章)

原文地址: http://www.code-studio.net/CSView.aspx?aid=157
下面的代码是出自孟子E章的,他用的是 ASP.NET 1.1 写的,但现在一般企业都转向 ASP.NET 2.0,为了方便新手,我就改为 ASP.NET 2.0了,希望孟子不要介意。:)

做为演示代码,程序里编程填充数据,不需要连接数据库。

文件名:Default3.aspx

.aspx
HTML CODE
<% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " Default3.aspx.cs " Inherits = " Default3 " %> 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:DataGrid ID ="DataGrid1" runat ="server" OnItemDataBound ="DataGrid1_ItemDataBound" > asp:DataGrid > < asp:Button ID ="Button1" runat ="server" OnClick ="Button1_Click" Text ="输出Excel" /> div > form > body > html >


.cs
C# CODE
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; public partial class Default3 : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { DataGrid1.DataSource = CreateDataSource(); DataGrid1.DataBind(); } ICollection CreateDataSource() { DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add( new DataColumn( " 身份证号码 " , typeof ( string ))); dt.Columns.Add( new DataColumn( " 图书单价 " , typeof ( decimal ))); dt.Columns.Add( new DataColumn( " 购买数量 " , typeof (Int32))); dt.Columns.Add( new DataColumn( " 总价格 " , typeof ( decimal ))); for ( int i = 0 ; i < 30 ; i ++ ) { dr = dt.NewRow(); dr[ 0 ] = " 123456789123456789 " ; dr[ 1 ] = 100 * i / 3.0 ; dr[ 2 ] = i + 5 ; dr[ 3 ] = ( decimal )dr[ 1 ] * (Int32)dr[ 2 ]; dt.Rows.Add(dr); } DataView dv = new DataView(dt); return dv; } protected void Button1_Click( object sender, EventArgs e) { Response.Clear(); Response.Buffer = true ; Response.Charset = " GB2312 " ; Response.AppendHeader( " Content-Disposition " , " attachment;filename=FileName.xls " ); Response.ContentEncoding = System.Text.Encoding.GetEncoding( " GB2312 " ); // 设置输出流为简体中文 Response.ContentType = " application/ms-excel " ; // 设置输出文件类型为excel文件。 this .EnableViewState = false ; System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo( " ZH-CN " , true ); System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this .DataGrid1.RenderControl(oHtmlTextWriter); Response.Write(oStringWriter.ToString()); Response.End(); } protected void DataGrid1_ItemDataBound( object sender, DataGridItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { e.Item.Cells[ 0 ].Attributes.Add( " style " , " vnd.ms-excel.numberformat:@ " ); e.Item.Cells[ 3 ].Attributes.Add( " style " , " vnd.ms-excel.numberformat:¥#,###.00 " ); } } }

你可能感兴趣的:(ASP。NET)