前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ASP.NET datable导出excel

ASP.NET datable导出excel

作者头像
跟着阿笨一起玩NET
发布2018-09-19 11:45:23
6070
发布2018-09-19 11:45:23
举报

本文转载:https://cloud.tencent.com/developer/article/1343179

不错的文章:https://cloud.tencent.com/developer/article/1342727

不错文章: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)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-07-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档