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

时间:2006/5/25 20:48:00      阅读:3434          

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

OutPutExcel.aspx

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

OutPutExcel.aspx.cs

  1using System;
  2using System.Collections;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Web;
  7using System.Web.SessionState;
  8using System.Web.UI;
  9using System.Web.UI.WebControls;
 10using System.Web.UI.HtmlControls;
 11
 12namespace eMeng.Exam
 13{
 14/// <summary>
 15/// OutPutExcel 的摘要说明。
 16/// </summary>
 17public class OutPutExcel : System.Web.UI.Page
 18{
 19protected System.Web.UI.WebControls.Button Button1;
 20protected System.Web.UI.WebControls.DataGrid DataGrid1;
 21
 22private void Page_Load(object sender, System.EventArgs e)
 23{
 24// 在此处放置用户代码以初始化页面
 25DataGrid1.DataSource=CreateDataSource();
 26DataGrid1.DataBind();
 27}
 28/// <summary>
 29/// 创建数据源
 30/// </summary>
 31/// <returns>DataView</returns>
 32ICollection CreateDataSource() 
 33{
 34
 35DataTable dt = new DataTable();
 36DataRow dr;
 37dt.Columns.Add(new DataColumn("身份证号码", typeof(string)));
 38dt.Columns.Add(new DataColumn("图书单价",typeof(decimal)));
 39dt.Columns.Add(new DataColumn("购买数量",typeof(Int32)));
 40dt.Columns.Add(new DataColumn("总价格",typeof(decimal)));
 41
 42
 43for (int i = 0; i < 30; i++) 
 44{
 45dr = dt.NewRow();
 46
 47dr[0] = "123456789123456789";
 48dr[1] = 100 * i /3.0;
 49dr[2] = i + 5;
 50dr[3] = (decimal)dr[1] * (Int32)dr[2];                
 51dt.Rows.Add(dr);
 52}
 53DataView dv = new DataView(dt);
 54return dv;
 55}
 56/// <summary>
 57/// 输出到Excel
 58/// </summary>
 59/// <param name="sender"></param>
 60/// <param name="e"></param>
 61private void Button1_Click(object sender, System.EventArgs e)
 62{
 63Response.Clear(); 
 64Response.Buffer= true; 
 65Response.Charset="GB2312";    
 66Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls"); 
 67Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
 68Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
 69this.EnableViewState = false;    
 70System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
 71System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); 
 72System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
 73this.DataGrid1.RenderControl(oHtmlTextWriter); 
 74Response.Write(oStringWriter.ToString());
 75Response.End();
 76}
 77
 78Web 窗体设计器生成的代码
100
101private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
102{
103if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
104{
105e.Item.Cells[0].Attributes.Add("style","vnd.ms-excel.numberformat:@");
106e.Item.Cells[3].Attributes.Add("style","vnd.ms-excel.numberformat:¥#,###.00");
107}
108}
109}
110}
111
112
评论
  • Re:DataGrid输出到Excel并进行格式化处理  (2006/6/12 12:31:04) by 鐜嬩腹 
    我保存的名字怎么能乱码了?
  • Re:DataGrid输出到Excel并进行格式化处理  (2006/6/12 12:31:23) by 王丹 
    看刚才的by后面的。
  • Re:DataGrid输出到Excel并进行格式化处理  (2006/6/12 16:10:45) by 踢西 
    乱码是否你没经编码处理?中文名请经过编码
  • Re:DataGrid输出到Excel并进行格式化处理  (2006/8/16 15:40:14) by 飘萍 
    我也发现评论保存名字后,有时候会出现乱码。
标 题:
 
姓 名:
 
主 页:

验证码:

评论:
 

Because of the cache,you may see your comments several minutes later.