DataGrid输出到Excel(word)并进行格式化处理(转)

<% @ Page language="c#" Codebehind="OutPutExcel.aspx.cs" AutoEventWireup="false" Inherits="eMeng.Exam.OutPutExcel"  %>
None.gif
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
None.gif
< HTML >
None.gif
< HEAD >
None.gif
< title > OutPutExcel </ title >     
None.gif
</ HEAD >
None.gif
< body >
None.gif
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif
< asp:DataGrid  id ="DataGrid1"  runat ="server" ></ asp:DataGrid >
None.gif
< asp:Button  id ="Button1"  runat ="server"  Text ="输出到Excel" ></ asp:Button >
None.gif
</ form >
None.gif
</ body >
None.gif
</ HTML >

OutPutExcel.aspx.cs

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
namespace  eMeng.Exam
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary>
InBlock.gif
/// OutPutExcel 的摘要说明。
ExpandedSubBlockEnd.gif
/// </summary>

InBlock.gifpublic class OutPutExcel : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
protected System.Web.UI.WebControls.Button Button1;
InBlock.gif
protected System.Web.UI.WebControls.DataGrid DataGrid1;
InBlock.gif
InBlock.gif
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
// 在此处放置用户代码以初始化页面
InBlock.gif
DataGrid1.DataSource=CreateDataSource();
InBlock.gifDataGrid1.DataBind();
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary>
InBlock.gif
/// 创建数据源
InBlock.gif
/// </summary>
ExpandedSubBlockEnd.gif
/// <returns>DataView</returns>

InBlock.gifICollection CreateDataSource() 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gifDataTable dt 
= new DataTable();
InBlock.gifDataRow dr;
InBlock.gifdt.Columns.Add(
new DataColumn("身份证号码"typeof(string)));
InBlock.gifdt.Columns.Add(
new DataColumn("图书单价",typeof(decimal)));
InBlock.gifdt.Columns.Add(
new DataColumn("购买数量",typeof(Int32)));
InBlock.gifdt.Columns.Add(
new DataColumn("总价格",typeof(decimal)));
InBlock.gif
InBlock.gif
InBlock.gif
for (int i = 0; i < 30; i++
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifdr 
= dt.NewRow();
InBlock.gif
InBlock.gifdr[
0= "123456789123456789";
InBlock.gifdr[
1= 100 * i /3.0;
InBlock.gifdr[
2= i + 5;
InBlock.gifdr[
3= (decimal)dr[1* (Int32)dr[2];                
InBlock.gifdt.Rows.Add(dr);
ExpandedSubBlockEnd.gif}

InBlock.gifDataView dv 
= new DataView(dt);
InBlock.gif
return dv;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary>
InBlock.gif
/// 输出到Excel
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif
/// <param name="e"></param>

InBlock.gifprivate void Button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifResponse.Clear(); 
InBlock.gifResponse.Buffer
= true
InBlock.gifResponse.Charset
="GB2312";    
InBlock.gifResponse.AppendHeader(
"Content-Disposition","attachment;filename=FileName.xls"); 
InBlock.gifResponse.ContentEncoding
=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
InBlock.gif
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
InBlock.gif
this.EnableViewState = false;    
InBlock.gifSystem.Globalization.CultureInfo myCItrad 
= new System.Globalization.CultureInfo("ZH-CN",true);
InBlock.gifSystem.IO.StringWriter oStringWriter 
= new System.IO.StringWriter(myCItrad); 
InBlock.gifSystem.Web.UI.HtmlTextWriter oHtmlTextWriter 
= new System.Web.UI.HtmlTextWriter(oStringWriter);
InBlock.gif
this.DataGrid1.RenderControl(oHtmlTextWriter); 
InBlock.gifResponse.Write(oStringWriter.ToString());
InBlock.gifResponse.End();
ExpandedSubBlockEnd.gif}

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//
InBlock.gif
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif
//
InBlock.gif
InitializeComponent();
InBlock.gif
base.OnInit(e);
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary>
InBlock.gif
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif
/// 此方法的内容。
ExpandedSubBlockEnd.gif
/// </summary>

InBlock.gifprivate void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{    
InBlock.gif
this.Button1.Click += new System.EventHandler(this.Button1_Click);
InBlock.gif
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
InBlock.gif
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif
#endregion

InBlock.gif
InBlock.gif
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gife.Item.Cells[
0].Attributes.Add("style","vnd.ms-excel.numberformat:@");
InBlock.gife.Item.Cells[
3].Attributes.Add("style","vnd.ms-excel.numberformat:¥#,###.00");
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif

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

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();



转载于:https://www.cnblogs.com/powerlc/archive/2005/01/05/87013.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值