DataGrid输出到Excel并进行格式化处理

 

OutPutExcel.aspx

& amp;lt; % @ Page language =& amp;quot;c# & amp;quot; Codebehind =& amp;quot;OutPutExcel.aspx.cs & amp;quot;
AutoEventWireup
=& amp;quot; false & amp;quot; Inherits =& amp;quot;eMeng.Exam.OutPutExcel & amp;quot;  %& amp;gt;
& amp;lt; ! DOCTYPE HTML PUBLIC  & amp;quot; - // W3C // DTD HTML 4.0 Transitional // EN" >
& amp;lt;HTML & amp;gt;
& amp;lt;HEAD & amp;gt;
& amp;lt;title & amp;gt;OutPutExcel & amp;lt; / title & amp;gt;
& amp;lt; / HEAD & amp;gt;
& amp;lt;body & amp;gt;
& amp;lt;form id =& amp;quot;Form1 & amp;quot; method =& amp;quot;post & amp;quot; runat =& amp;quot;server & amp;quot; & amp;gt;
& amp;lt;asp:DataGrid id =& amp;quot;DataGrid1 & amp;quot; runat =& amp;quot;server & amp;quot; & amp;gt; & amp;lt; / asp:DataGrid & amp;gt;
& amp;lt;asp:Button id =& amp;quot;Button1 & amp;quot; runat =& amp;quot;server & amp;quot; Text =& amp;quot;输出到Excel & amp;quot; & amp;gt; & amp;lt; / asp:Button & amp;gt;
& amp;lt; / form & amp;gt;
& amp;lt; / body & amp;gt;
& amp;lt; / HTML & amp;gt;
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");
}

}

}

}

 

 

 

 

//直接把Dataset内容转换到Excel
       //crl为任意控件
    //if想输入固定格式的excel,先用datagrid做好模版,放到panel里,输出panel就ok
        
public   static   void  ToExcel(System.Web.UI.Control ctl, string  FileName)
        
{
            HttpContext.Current.Response.Charset 
="UTF-8";
            HttpContext.Current.Response.ContentEncoding 
=System.Text.Encoding.Default;
            HttpContext.Current.Response.ContentType 
="application/ms-excel";
            HttpContext.Current.Response.AppendHeader(
"Content-Disposition","attachment;filename="+""+FileName+".xls");
            ctl.Page.EnableViewState 
=false;
            System.IO.StringWriter  tw 
= new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw 
= new System.Web.UI.HtmlTextWriter(tw);
            ctl.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
        }
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值