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

  

ASP.NET datable导出excel的更多相关文章

  1. asp.net中导出Excel的方法

    一.asp.net中导出Excel的方法: 本文转载 在asp.net中导出Excel有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上:一种是将文件直接将文件输出 ...

  2. ASP.net中导出Excel的简单方法介绍

    下面介绍一种ASP.net中导出Excel的简单方法 先上代码:前台代码如下(这是自己项目里面写的一点代码先贴出来吧) <div id="export" runat=&quo ...

  3. 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格式不一致的解决办法

    -----转载:http://blog.csdn.net/sgear/article/details/7663502 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格 ...

  4. ASP.NET MVC导出excel

    ASP.NET MVC导出excel 要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式 ...

  5. C# asp.net中导出Excel表时总出现"只能在执行 Render() 的过程中调用 RegisterForEventValidation

    C# asp.net中导出Excel表时总出现"只能在执行 Render() 的过程中调用 RegisterForEventValidation 后台添加以下方法:/// <summa ...

  6. ASP.NET MVC导出excel(数据量大,非常耗时的,异步导出)

    要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式,后台开辟一个线程将excel导出到指 ...

  7. Asp.net中导出Excel文档(Gridview)

    主要思路,通过GridView来导出文档. 新建一个Aspx页面,页面创建GridView控件,后台绑定好数据源.然后load中直接打印即可导出 前台的GridView <asp:GridVie ...

  8. Asp.net Gridview导出Excel

    前台页面放一个GridView什么的就不说了,要注意的是在 <%@ Page Language="C#" AutoEventWireup="true" C ...

  9. ASP.NET导入导出Excel方法大全

    本文介绍下,C#实现的可以导出与导入excel的代码一例,有需要的朋友,参考下吧. C#实现导出与导入excel.代码1: 复制代码 代码示例:#region  导出Excel  /// <su ...

随机推荐

  1. 【实习记】2014-08-18使用curl排错http头的content-length

        总结一,用curl排错Content-Length设置错误,误导了客户端. 访问/cgi-bin/txproj_list时,firebug显示总是不多不少15秒,调试其他问题时郁闷. fire ...

  2. 用QT 还是MFC ? (转)

    我曾经使用过QT和MFC来开发过软件,我想和大家分享我使用他们时所体会的不同之处. 我并非一个职业作家,这篇文章可能看起来不如专业的杂志和网站上的那么条理清晰.但是,我在这里是用我自己的语言来表达我自 ...

  3. Java:对象的序列化

    一.对象序列化机制 序列化机制允许将实现序列化的Java对象转换为字节序列,这些字节序列可以被保存在磁盘上或通过网络传输,以备以后重新恢复原来的对象: 序列化机制使得对象可以脱离程序的运行而独立存在: ...

  4. 生产环境CentOS服务器系统安全配置

    转http://www.centoscn.com/CentosSecurity/CentosSafe/2014/1126/4192.html 账户安全及权限 禁用root以外的超级用户 删除不必要的账 ...

  5. JS-运动框架

    写这段代码,是因为之前看过某前RD写过,但在测试过程中发现有不完美的地方. 问题在于判断运动停止条件这里,对于之前停止的判断太片面,只能判断一个条件值时的情况,对于多条件时,会发现运动后的各项值并未达 ...

  6. 软件测试software testing summarize

    软件测试(英语:software testing),描述一种用来促进鉴定软件的正确性.完整性.安全性和质量的过程.软件测试的经典定义是:在规定的条件下对程序进行操作,以发现程序错误,衡量软件质量,并对 ...

  7. secureCRT使用小贴士

    (一)使用WIN键盘 在securecrt界面:工具→键映射编辑器,在弹出的键盘中点击“home”,会弹出一个窗口,在“发送字符串”中输入:\033[1~ 另存为securecrt安装根目录下的Key ...

  8. 微信JS-SDK签名signature错误代码4029

    一般是url的错,url需要动态获取 c#的写法: //Request.Url.ToString()这个获取的是没有端口号的有些网站有端口号 string url = Request.Url.Orig ...

  9. php的几个版本的区别?

    1. VC6与VC9的区别:VC6版本是使用Visual Studio 6编译器编译的,如果你的PHP是用Apache来架设的,那你就选择VC6版本.VC9版本是使用Visual Studio 200 ...

  10. ASP.NET MVC 学习

    项目结构