DataGrid輸出到Excel並進行格式化處理

來源:互聯網
上載者:User
datagrid|excel

在我們把DataGrid上的資料匯入到Excel的時候,如果遇到比較長的數字字串,比如社會安全號碼碼,就會在Excel裡當成數字看待,並轉換成科學計數法的格式,造成資料的丟失,下面這個方法就解決了這個問題,並樣本如何進行其它的格式化。

查看例子

OutPutExcel.aspx

<%@ 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{/// &lt;summary&gt;/// OutPutExcel 的摘要說明。/// &lt;/summary&gt;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();}/// &lt;summary&gt;/// 建立資料來源/// &lt;/summary&gt;/// &lt;returns&gt;DataView&lt;/returns&gt;ICollection CreateDataSource() {DataTable dt = new DataTable();DataRow dr;dt.Columns.Add(new DataColumn(&quot;社會安全號碼碼&quot;, typeof(string)));dt.Columns.Add(new DataColumn(&quot;圖書單價&quot;,typeof(decimal)));dt.Columns.Add(new DataColumn(&quot;購買數量&quot;,typeof(Int32)));dt.Columns.Add(new DataColumn(&quot;總價格&quot;,typeof(decimal)));for (int i = 0; i &lt; 30; i++) {dr = dt.NewRow();dr[0] = &quot;123456789123456789&quot;;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;}/// &lt;summary&gt;/// 輸出到Excel/// &lt;/summary&gt;/// &lt;param name=&quot;sender&quot;&gt;&lt;/param&gt;/// &lt;param name=&quot;e&quot;&gt;&lt;/param&gt;private void Button1_Click(object sender, System.EventArgs e){Response.Clear(); Response.Buffer= true; Response.Charset=&quot;GB2312&quot;; Response.AppendHeader(&quot;Content-Disposition&quot;,&quot;attachment;filename=FileName.xls&quot;); Response.ContentEncoding=System.Text.Encoding.GetEncoding(&quot;GB2312&quot;);//設定輸出資料流為簡體中文Response.ContentType = &quot;application/ms-excel&quot;;//設定輸出檔案類型為excel檔案。 this.EnableViewState = false; System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo(&quot;ZH-CN&quot;,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();}#region Web Form設計器產生的程式碼override protected void OnInit(EventArgs e){//// CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。//InitializeComponent();base.OnInit(e);}/// &lt;summary&gt;/// 設計器支援所需的方法 - 不要使用代碼編輯器修改/// 此方法的內容。/// &lt;/summary&gt;private void InitializeComponent(){ this.Button1.Click += new System.EventHandler(this.Button1_Click);this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);this.Load += new System.EventHandler(this.Page_Load);}#endregionprivate 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(&quot;style&quot;,&quot;vnd.ms-excel.numberformat:@&quot;);e.Item.Cells[3].Attributes.Add(&quot;style&quot;,&quot;vnd.ms-excel.numberformat:¥#,###.00&quot;);}</p> <CENTER> </CENTER> <br /><br />
相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。