<%@ 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("身份证号码"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 窗体设计器生成的代码

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");
}

}

}

}



--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//***********************************************************************************************************************************************
说明
一、定义文档类型、字符编码

Response.Clear();
Response.Buffer= true;
Response.Charset="utf-8";

//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开

//filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc || .xls || .txt ||.htm

Response.AppendHeader("Content-Disposition","attachment;filename=FileFlow.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");

//Response.ContentType指定文件类型 可以为application/ms-excel || application/ms-word || application/ms-txt || application/ms-html || 或其他浏览器可直接支持文档

Response.ContentType = "application/ms-excel";
this.EnableViewState = false;

二、定义一个输入流

System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

三、将目标数据绑定到输入流输出

this.RenderControl(oHtmlTextWriter);

//this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件

Response.Write(oStringWriter.ToString());
Response.End();



posted on 2005-01-05 22:02  powerlc  阅读(912)  评论(0编辑  收藏  举报