【转贴】DataGrid输出到Excel并进行格式化处理

【转贴】DataGrid输出到Excel并进行格式化处理

在我们把DataGrid上的数据导入到Excel的时候,如果遇到比较长的数字字符串,比如身份证号码,就会在Excel里当成数字看待,并转换成科学计数法的格式,造成数据的丢失,下面这个方法就解决了这个问题,并示例如何进行其它的格式化。

查看例子:http://dotnet.aspx.cc/Exam/OutPutExcel.aspx

OutPutExcel.aspx

 1 <%@ Page language="c#" Codebehind="OutPutExcel.aspx.cs" AutoEventWireup="false" Inherits="eMeng.Exam.OutPutExcel" %>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
 3 <HTML>
 4 <HEAD>
 5 <title>OutPutExcel</title>    
 6 </HEAD>
 7 <body>
 8 <form id="Form1" method="post" runat="server">
 9 <asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
10 <asp:Button id="Button1" runat="server" Text="输出到Excel"></asp:Button>
11 </form>
12 </body>
13 </HTML>
14 
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}

转载于:https://www.cnblogs.com/frank1223/archive/2006/12/15/593392.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值