导出数据到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. Swift - 20 - 字典的基础操作

    //: Playground - noun: a place where people can play import UIKit var dict = [1:"one", 2:& ...

  2. Swift - 02 - 常量和变量

    //: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...

  3. 05_Smart-image通过SoftReference提高性能

    文章导读: 文件介绍了常见的图片下载开源插件smart-image, 由于移动设备硬件受限,因此Android的相关app都要考虑到性能的关系, 所以很多的第三方插件都使用到了缓存cache技术,本人 ...

  4. 深入理解offsetTop与offsetLeft

    做为走上前端不归路的我,以前只是认为offsetTop是元素的左边框至包含元素offsetParent的左内边框之间的像素距离,同理offsetRight是相对于上内边框.那么问题来了,包含元素off ...

  5. Win7下Solr4.10.1和MySql的整合(索引与搜索)

    1.打开D:\webserver\solr\collection1\conf\solrconfig.xml文件,在<requestHandler name="/select" ...

  6. 88 Merge Sorted Array(归并排序Easy)

    题目意思:num1和num2均为递增数组,对其进行递增排序存到num1中 class Solution { public: void merge(vector<int>& nums ...

  7. VISUAL STUDIO 2005连接MYSQL数据库

    // mysql.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <string.h> #include &l ...

  8. Python学习笔记六--文件和输入输出

    6.1文件对象 所有Python对文件的操作都是基于对文件对象的操作.那么就从文件对象的创建说起.open()[file()]提供初始化输入输出的接口.open()成功打开文件时会返回一个文件对象. ...

  9. int string convert

    C++ int与string的转化 int本身也要用一串字符表示,前后没有双引号,告诉编译器把它当作一个数解释.缺省 情况下,是当成10进制(dec)来解释,如果想用8进制,16进制,怎么办?加上前缀 ...

  10. C题

    C - C Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description   Ass ...