ASP.NET datable导出excel

简介: 本文转载:http://www.cnblogs.com/chwkai/archive/2005/10/08/250426.html 不错的文章:http://www.cnblogs.com/lzhp/archive/2012/08/02/2680763.

本文转载: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)

  

目录
相关文章
|
1月前
|
NoSQL 关系型数据库 MySQL
多人同时导出 Excel 干崩服务器?怎样实现一个简单排队导出功能!
业务诉求:考虑到数据库数据日渐增多,导出会有全量数据的导出,多人同时导出可以会对服务性能造成影响,导出涉及到mysql查询的io操作,还涉及文件输入、输出流的io操作,所以对服务器的性能会影响的比较大;结合以上原因,对导出操作进行排队; 刚开始拿到这个需求,第一时间想到就是需要维护一个FIFO先进先出的队列,给定队列一个固定size,在队列里面的人进行排队进行数据导出,导出完成后立马出队列,下一个排队的人进行操作;还考虑到异步,可能还需要建个文件导出表,主要记录文件的导出情况,文件的存放地址,用户根据文件列表情况下载导出文件。
多人同时导出 Excel 干崩服务器?怎样实现一个简单排队导出功能!
|
3月前
|
Java Apache Spring
springboot如何导出Excel某个表的表字段以及字段类型
springboot如何导出Excel某个表的表字段以及字段类型
32 0
|
3月前
|
关系型数据库 MySQL 数据库连接
python查询数据库的某个表,将结果导出Excel
python查询数据库的某个表,将结果导出Excel
46 0
|
2月前
|
XML Java 数据格式
使用Freemarker模版导出xls文件使用excel打开提示文件损坏
使用Freemarker模版导出xls文件使用excel打开提示文件损坏
50 0
|
1月前
|
SQL Java easyexcel
【Java】百万数据excel导出功能如何实现
【Java】百万数据excel导出功能如何实现
120 0
|
2月前
|
Java
使用POI导出Excel
使用POI导出Excel
|
2月前
|
前端开发 Java Maven
springboot优雅的实现excel的导出(自适应列宽实现,中文也行),复制可用
springboot优雅的实现excel的导出(自适应列宽实现,中文也行),复制可用
58 0
|
2月前
|
测试技术 数据处理 Python
测试报告导出PDF和excel的方法
测试报告导出PDF和excel的方法
|
3月前
|
Python
python如何导出Excel某个表的表字段以及字段类型
python如何导出Excel某个表的表字段以及字段类型
24 0