C#将DataTable导出Execl、Word、Xml
/// <summary>
/// 将DT转换为Execl的方法
/// </summary>
/// <param name="dt">需要导出的DT
/// <param name="page">页面
/// <param name="fileName">文件名
public void ToExecl(DataTable dt, Page page, string fileName)
{
HttpResponse response = page.Response;
response.Clear();
response.ContentType = "application/x-excel";
response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
StringBuilder sB = new StringBuilder();
for (int j = ; j < dt.Columns.Count; j++)
{
sB.Append(dt.Columns[j].Caption + "\t");
}
sB.Append("\n");
for (int i = ; i < dt.Rows.Count; i++)
{
for (int k = ; k < dt.Columns.Count; k++)
{
sB.Append("=\"" + dt.Rows[i][k].ToString() + "\"\t"); //解决导出的单元格以科学计数法显示的问题
}
sB.Append("\n");
}
response.Write(sB.ToString());
response.End();
} public void ToWord(DataTable dt, Page page, string filName)
{
HttpResponse response = page.Response;
response.Clear();
response.ContentType = "application/msword";
response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
response.AddHeader("Content-Disposition","attachment:filename="+System.Web.HttpUtility.UrlEncode(filName,System.Text.Encoding.UTF8)+".doc");
StringBuilder sBuilder = new StringBuilder();
for (int i = ; i < dt.Rows.Count; i++)
{
sBuilder.Append(dt.Rows[i][].ToString()+"\n");
}
response.Write(sBuilder.ToString());
response.End();
} public void ToXML(DataTable dt, Page page, string filename)
{
HttpResponse response = page.Response;
//DataSet ds = new DataSet();
response.Clear();
response.ContentType = "application/x-excel";
response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename,System.Text.Encoding.UTF8) + ".xls");
System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding();
System.Xml.XmlTextWriter xw = new XmlTextWriter(response.OutputStream, utf8);
xw.Formatting = Formatting.Indented;
xw.Indentation = ;
xw.IndentChar = ' ';
dt.TableName = "dd";
dt.WriteXml(xw);
dt = null;
GC.Collect();
xw.Flush();
xw.Close();
response.End();
}
来源:http://blog.csdn.net/smartsmile2012/article/details/8182862
C#将DataTable导出Execl、Word、Xml的更多相关文章
- c#(NPOI)DataTable导出execl,execl(支持解析公式)导入DataTable
NPOI(C#)DataTable导出execl using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserMod ...
- datatable导出到Word / Excel / PDF / HTML .NET
原文发布时间为:2011-01-21 -- 来源于本人的百度文章 [由搬家工具导入] IEnumerable - DataTable Export to Word / Excel / PDF / HT ...
- DataTable导出为word,excel,html,csv,pdf,.txt
using System; using System.Data; using System.Configuration; using System.Collections; using System. ...
- 关于datatable导出execl
导出主要考虑响应流的问题 curContext.Response.ContentType = "application/vnd.ms-excel"; curContext.Resp ...
- .net中将DataTable导出到word、Excel、txt、htm的方法
dt:DataTable strFile:fileName strExt:type private void GridExport(DataTable dt, string strFile, stri ...
- asp.net中导出Execl的方法
一.asp.net中导出Execl的方法: 在 asp.net中导出Execl有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址 输出在浏览器上:一种是将文件直接将文件输出流写给 ...
- DataTable导出到Excel
简单的导出到Excel中: 代码如下: using System; using System.Collections.Generic; using System.Data; using System. ...
- Java 实现HTML富文本导出至word完美解决方案
一. 问题的提出 最近用java开发一个科技项目信息管理系统,里面有一个根据项目申请书的模板填写项目申报信息的功能,有一个科技项目申请书word导出功能. 已有的实现方式:采用标准的jsp模板输出实现 ...
- java导出生成word(类似简历导出)
参考帖子: http://www.cnblogs.com/lcngu/p/5247179.html http://www.cnblogs.com/splvxh/archive/2013/03/15/2 ...
随机推荐
- "NetworkError: 500 Internal Server Error - http://develop.console.aliyun.sinopec.com/ots/ots_queryOtsList.action?state=0"
项目明明开始好好的,报的这个错,错误提示也很少,啥信息都没有,只是明白是服务器报了500,知道是服务器内部错误,但是却没法找不到问题所在.后来突然想到把下面报错的action直接在浏览器运行: htt ...
- Web API路由与动作(三)
本章包括三个小节 如果你输入了mvc的路由规则 这个可以粗略过一遍即可 内容说明有点繁琐 原文地址:http://www.asp.net/web-api/overview/web-api-rout ...
- CTO的眼界到底有多宽?
CTO的眼界到底有多宽? [CSDN独家报道]在各大企业中,CTO (Chief Technology Officer,首席技术官)有着雄厚的技术实力,掌握着企业核心技术,是软件开发项目中最重要的人 ...
- maven多配目配置总结
一. 设置一个parent项目,parent项目的作用应该有如下方面: 1.统一管理jar包的版本,这样许多子项目只需指定依赖的jar,而无需指定所依赖jar的版本 (1)在<propertie ...
- PrintWriter out = response.getWriter() 输出中文乱码问题
HttpServletResponse response = ServletActionContext.getResponse(); response.setCharacterEncod ...
- 全面理解Unity加载和内存管理
全面理解Unity加载和内存管理http://game.ceeger.com/forum/read.php?tid=4394&fid=2&uid=6507 1.用简单的“for”循环 ...
- Delphi TcxTreeList 节点添加图片
需要给TcxTreelist的列添加图片,操作如下 1.设置列, 设置Properties为ImageComboBox , 2. 设置Properties -> Items 添加内容 对应的增加 ...
- HDU --1251
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- Modifying the ASP.NET Request Queue Limit
Modifying the ASP.NET Request Queue Limit When ASP.NET is queried, the request for service is carrie ...
- 2D游戏编程5—锁定频率
核心利用win心跳函数GetTickCount利用差量锁定fps,如下代码锁定30fps,缺点为如果计算机不能以30fps运行,程序将低于30fps #define WIN32_LEAN_AND_ME ...