导出数据到Excel通用的方法类,请应对需求自行修改。

资源下载列表

using System.Data;
using System.IO; namespace IM.Common.Tools
{
public class Export
{
public string Encoding = "UTF-8";
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; public void EcportExcel(DataTable dt, string fileName)
{ if (dt != null)
{
StringWriter sw = new StringWriter();
CreateStringWriter(dt, ref sw);
sw.Close();
response.Clear();
response.Buffer = true;
response.Charset = Encoding;
//this.EnableViewState = false;
response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
response.ContentType = "application/ms-excel"; //response.ContentEncoding = System.Text.Encoding.GetEncoding(Encoding)
response.ContentEncoding = System.Text.Encoding.UTF8;
response.Write(sw);
response.End();
} } private void CreateStringWriter(DataTable dt, ref StringWriter sw)
{
string sheetName = "sheetName"; sw.WriteLine("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
sw.WriteLine("<head>");
sw.WriteLine("<!--[if gte mso 9]>");
sw.WriteLine("<xml>");
sw.WriteLine(" <x:ExcelWorkbook>");
sw.WriteLine(" <x:ExcelWorksheets>");
sw.WriteLine(" <x:ExcelWorksheet>");
sw.WriteLine(" <x:Name>" + sheetName + "</x:Name>");
sw.WriteLine(" <x:WorksheetOptions>");
sw.WriteLine(" <x:Print>");
sw.WriteLine(" <x:ValidPrinterInfo />");
sw.WriteLine(" </x:Print>");
sw.WriteLine(" </x:WorksheetOptions>");
sw.WriteLine(" </x:ExcelWorksheet>");
sw.WriteLine(" </x:ExcelWorksheets>");
sw.WriteLine("</x:ExcelWorkbook>");
sw.WriteLine("</xml>");
sw.WriteLine("<![endif]-->");
sw.WriteLine("</head>");
sw.WriteLine("<body>");
sw.WriteLine("<table>");
sw.WriteLine(" <tr>");
string[] columnArr = new string[dt.Columns.Count];
int i = ;
foreach (DataColumn columns in dt.Columns)
{ sw.WriteLine(" <td>" + columns.ColumnName + "</td>");
columnArr[i] = columns.ColumnName;
i++;
}
sw.WriteLine(" </tr>"); foreach (DataRow dr in dt.Rows)
{
sw.WriteLine(" <tr>");
foreach (string columnName in columnArr)
{
sw.WriteLine(" <td style='vnd.ms-excel.numberformat:@'>" + dr[columnName] + "</td>");
}
sw.WriteLine(" </tr>");
}
sw.WriteLine("</table>");
sw.WriteLine("</body>");
sw.WriteLine("</html>");
}
}
}

C#导出数据到Excel通用的方法类的更多相关文章

  1. 一个很好的用C#导出数据到Excel模板的方法

    /// <summary> /// 导数据到Excel模板 /// </summary> /// <param name="tab">要输出内容 ...

  2. 从DataTable高效率导出数据到Excel

    首先从数据库读取数据到DataTable,这我就不提了,大家都明白.下面直接介绍如何从DataTable高效率导出数据到Excel中的方法,代码如下: using Microsoft.Office.I ...

  3. MVC导出数据到EXCEL新方法:将视图或分部视图转换为HTML后再直接返回FileResult

    导出EXCEL方法总结 MVC导出数据到EXCEL的方法有很多种,常见的是: 1.采用EXCEL COM组件来动态生成XLS文件并保存到服务器上,然后转到该文件存放路径即可: 优点:可设置丰富的EXC ...

  4. 一个方便且通用的导出数据到 Excel 的类库

    一个方便且通用的导出数据到 Excel 的类库 起源: 之前在做一个项目时,客户提出了许多的导出数据的需求: 导出用户信息 导出业务实体信息 各种查询都要能导出 导出的数据要和界面上看到的一致 可以分 ...

  5. php导出数据到excel,防止身份证等数字字符格式变成科学计数的方法

    而关于php的也有,但是大多都是用phpExcel导出的方法或者spreadsheet等类或者控件之类的导出方法,而我所在维护的系统却用很简单的方法,如下,网上很少有讲如何设置要导出数据的EXcel格 ...

  6. Delphi 导出数据至Excel的7种方法【转】

    一; delphi 快速导出excel   uses ComObj,clipbrd;   function ToExcel(sfilename:string; ADOQuery:TADOQuery): ...

  7. 系统导出数据到excel,数据量过大(大约10W)条,导致服务器 cpu 100%解决方法

    系统导出数据到excel,数据量过大(大约10W)条,导致服务器 cpu 100%解决方法

  8. pl/sql developer导出数据到excel的方法

    http://yedward.net/?id=92 问题说明:使用pl/sql developer导出数据到excel表格中是非常有必要的,一般的可能直接在导出的时候选择csv格式即可,因为该格式可以 ...

  9. 导出数据到Excel方法总结

    一,问题的提出 近来在网上经常有人问怎样把数据导出到Excel中?针对这个问题网上也有很多资料.大都比较的琐碎.本人当前从事的项目中,刚好涉及到这些内容.就顺便做了一些归纳整理.共享给大家.避免大家再 ...

随机推荐

  1. sublime text There are no packages 解决!

    1.问题如下图 解决如下: 1.取得sublime.wbond.net的IPv4地址.在命令提示符中输入以下命令: ping sublime.wbond.net 获得  pv 4 ip     2.C ...

  2. [转]memmove函数

    [FROM MSDN && 百科] 原型:  void *memmove( void* dest, const void* src, size_tcount ); #include&l ...

  3. VS2012 ActiveX控件_D接口添加方法事项

    自己写的是Clock控件,所以控件的接口是_DClock 使用向导添加方法后,会在紫色区域自动生成红色代码:(添加Hello方法) dispinterface _DClock { properties ...

  4. js学习笔记之:数组(二)

    今天来学习一下数组的遍历.删除等知识点:    1 数组的遍历 数组元素的遍历可以使用for循环,采用关键字for...in var aCity =  new Array("北京" ...

  5. 修改PHP的默认时区

    每个地区都有自己的本地时间,在网上及无线电通信中,时间的转换问题显得格外突出.整个地球分为24个时区,每个时区都有自己的本地时间.在国际无线电或网络通信场合,为了统一起见,使用一个统一的时间,成为通用 ...

  6. MySql存储过程—3、变量

    1.变量的定义 在Mysql里面可以像我们写代码中一样定义变量来保持中间结果,看下面的格式: DECLARE variable_name datatype(size) DEFAULT default_ ...

  7. Python爬虫预备知识

    1.http编程知识 http中client 和server的工作模式 client和server建立可靠的tcp链接(在HTTP1.1中这个链接是长时间的,超时断开策略) client通过socke ...

  8. lua学习-1

    最近打算学习quick cocos2dx,所以首先打算学习一下lua这门语言,Lua 是一个小巧的脚本语言,轻量级,便于扩展. Lua脚本基本的数据类型:nil.boolean.number.stri ...

  9. 一次完整的HTTP事务是怎样一个过程?(转)

    (转自http://www.linux178.com/web/httprequest.html)写的太好了,转一个. 关于HTTP协议可以参考以下: HTTP协议漫谈 http://kb.cnblog ...

  10. CentOS 基础安装

    1. 下载了 CentOS 的最小安装版本 与 VMware,基础安装流程参考百度经验:http://jingyan.baidu.com/article/eae0782787b4c01fec54853 ...