ASP.NET datable匯出excel

來源:互聯網
上載者:User

標籤:style   blog   http   os   檔案   io   for   re   

本文轉載:http://www.cnblogs.com/chwkai/archive/2005/10/08/250426.html

不錯的文章:http://www.cnblogs.com/lzhp/archive/2012/08/02/2680763.html

不錯文章:http://hcyu2012.blog.163.com/blog/static/165192580201132532526918/

 public void DataTable2Excel(DataTable dtData)        {            if (dtData != null)            {                Response.Clear();                Response.Charset = "utf-8";                Response.AppendHeader("Content-Disposition", "attachment;filename= " + Server.UrlEncode("匯出.xls"));                Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");                Response.ContentType = "application/ms-excel";//設定輸出檔案類型為excel檔案.                this.EnableViewState = false;                System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);                System.IO.StringWriter stringWrite = new System.IO.StringWriter(myCItrad);                System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);                //// 為瞭解決dgData中可能進行了分頁的情況,需要重新定義一個無分頁的DataGrid                //System.Web.UI.WebControls.GridView dgExport = null;                //dgExport = new System.Web.UI.WebControls.GridView();                GridView dgExport = new GridView();                dgExport.AllowPaging = false;                dgExport.DataSource = dtData;                dgExport.DataBind();                for (int i = 1; i < dgExport.Rows.Count; i++)                {                                        dgExport.Rows[i].Cells[0].Attributes.Add("style", "mso-number-format:‘0‘");                    dgExport.Rows[i].Cells[2].Attributes.Add("style", "mso-number-format:‘\\@‘");                }                                dgExport.RenderControl(htmlWrite);                Response.Write(stringWrite.ToString());               Response.Write(@"<style> .text { mso-number-format:‘\\@‘;} </script> ");                Response.End();               }        }  GridView export to excel 格式參考mso-number-format:"0" NO Decimals mso-number-format:"0\.000" 3 Decimals mso-number-format:"\#\,\#\#0\.000" Comma with 3 dec mso-number-format:"mm\/dd\/yy" Date7 mso-number-format:"mmmm\ d\,\ yyyy" Date9 mso-number-format:"m\/d\/yy\ h\:mm\ AM\/PM" D -T AMPM mso-number-format:"Short Date" 01/03/1998 mso-number-format:"Medium Date" 01-mar-98 mso-number-format:"d\-mmm\-yyyy" 01-mar-1998 mso-number-format:"Short Time" 5:16 mso-number-format:"Medium Time" 5:16 am mso-number-format:"Long Time" 5:16:21:00 mso-number-format:"Percent" Percent - two decimals mso-number-format:"0%" Percent - no decimals mso-number-format:"0\.E+00" Scientific Notation mso-number-format:"\@" Text mso-number-format:"\#\ ???\/???" Fractions - up to 3 digits (312/943) mso-number-format:"\0022?\0022\#\,\#\#0\.00" ?12.76 mso-number-format:"\#\,\#\#0\.00_ \;\[Red\]\-\#\,\#\#0\.00\ " 2 decimals, negative numbers in red and signed(1.56   -1.56)

  

相關文章

聯繫我們

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

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.