//提示:導入到Excel是一張完整的表 如果大家做的有分頁的話 也許會報錯誤 自己可以修改下
---OutPutExcel.ASPx
<%@ Page language="c#" Codebehind="OutPutExcel.ASPx.cs"
AutoEventWireup="false" Inherits="eMeng.Exam.OutPutExcel" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD Html 4.0 Transitional//EN" >
<Html>
<HEAD>
<title>OutPutExcel</title>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" runat="server"></ASP:DataGrid>
<asp:Button id="Button1" runat="server" Text="輸出到Excel"></ASP:Button>
</form>
</body>
</Html>
----OutPutExcel.ASPx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace eMeng.Exam
...{
/**//// <summary>
/// OutPutExcel 的摘要說明。
/// </summary>
public class OutPutExcel : System.Web.UI.Page
...{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
...{
// 在此處放置用戶代碼以初始化頁面
DataGrid1.DataSource=CreateDataSource();
DataGrid1.DataBind();
}
/**//// <summary>
/// 創建數據源
/// </summary>
/// <returns>DataVIEw</returns>
ICollection CreateDataSource()
...{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("SFZ號碼", 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;
}
/**//// <summary>
/// 輸出到Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button1_Click(object sender, System.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();
}
Web 窗體設計器生成的代碼#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 該調用是 ASP.Net Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
...{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.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");
}
}
}
}