代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
DataTable导出为word,excel,html,csv,pdf,.txt
作者:
Dezai.CN
/ 发布于
2012/6/26
/
876
DataTable导出为word,excel,html,csv,pdf,.txt
<div> using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Collections.Generic; //using iTextSharp.text; //using iTextSharp.text.pdf; using System.IO; using System.Text; //using iTextSharp.text.html; using System.Xml; using iTextSharp; using iTextSharp.text; using iTextSharp.text.pdf; using System.Reflection; namespace zjf.Utility { public class Print { /// <summary> //导出word文件 /// </summary> /// <param name="FileType"></param> /// <param name="FileName"></param> public void ExportToDoc(string FileName, System.Web.UI.Control control) { string strFileName = System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName + ".doc"); HttpContext.Current.Response.ContentType = "application/ms-word"; control.EnableViewState = false; System.IO.StringWriter swOut = new System.IO.StringWriter(); HtmlTextWriter hTw = new HtmlTextWriter(swOut); control.RenderControl(hTw); HttpContext.Current.Response.Write(swOut.ToString());//////////////////////去除字符 HttpContext.Current.Response.End(); } /// <summary> /// 导出EXCEl文件 /// </summary> /// <param name="FileType"></param> /// <param name="FileName"></param> public void ExportToExcel(string FileName, System.Web.UI.Control control) { string strFileName = System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8); HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName + ".xls"); HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword control.Page.EnableViewState = false; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); control.RenderControl(hw); HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\">"); HttpContext.Current.Response.Write(tw.ToString().Trim());//////////////////////去除字符 HttpContext.Current.Response.End(); System.Web.HttpContext.Current.Response.End(); } /// <summary> /// 导出html ................... /// </summary> /// <param name="FileName"></param> /// <param name="control"></param> public void ExportTohtml(string FileName, System.Web.UI.Control control) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; string strFileName = System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8); HttpContext.Current.Response.Charset = "GB2312"; //Response.Charset = "GB2312"; HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312"); //Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString()); HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName + ".htm"); //Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString()); HttpContext.Current.Response.ContentType = "application/ms-html"; ; control.EnableViewState = false; System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); //GridView1.RenderControl(hw); HttpContext.Current.Response.Output.Write(tw.ToString()); HttpContext.Current.Response.Flush(); control.RenderControl(oHtmlTextWriter); HttpContext.Current.Response.Write(oStringWriter.ToString()); HttpContext.Current.Response.End(); } /// <summary> /// 导出CSV /// </summary> /// <param name="FileName"></param> /// <param name="control"></param> public void ExportTocsv(string FileName, DataSet ds) { string strFileName = System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8); string data = ExportCSV(ds); string temp = string.Format("attachment;filename={0}", strFileName + ".csv"); // Response.ClearHeaders(); HttpContext.Current.Response.AppendHeader("Content-disposition", temp); HttpContext.Current.Response.Write(data); HttpContext.Current.Response.End(); } /// <summary> /// 将DataSet导出成CSV格式 /// </summary> /// <param name="ds">DataSet</param> /// <returns>CSV字符串数据</returns> public static string ExportCSV(DataSet ds) { string data = ""; //data = ds.DataSetName + "\n"; foreach (DataTable tb in ds.Tables) { data += tb.TableName + "\n"; //写出列名 foreach (DataColumn column in tb.Columns) { data += column.ColumnName + ","; } data += "\n"; //写出数据 foreach (DataRow row in tb.Rows) { foreach (DataColumn column in tb.Columns) { data += row[column].ToString() + ","; } data += "\n"; } data += "\n"; } return data; } public void ExportPDF(DataTable datatable) { try { Document document = new Document(); PdfWriter.GetInstance(document, new FileStream("Chap0101.pdf", FileMode.Create)); document.Open(); BaseFont bfChinese = BaseFont.CreateFont("C:WINDOWSFontssimsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, new Color(0, 0, 0)); // document.Add(new Paragraph(this.TextBox1.Text.ToString(), fontChinese)); // iTextSharp.text.Image jpeg = iTextSharp.text.Image.GetInstance(Server.MapPath("pic015.jpg")); // document.Add(jpeg); PdfPTable table = new PdfPTable(datatable.Columns.Count); for (int i = 0; i < datatable.Rows.Count; i++) { for (int j = 0; j < datatable.Columns.Count; j++) { table.AddCell(new Phrase(datatable.Rows[i][j].ToString(), fontChinese)); } } document.Add(table); document.Close(); } catch (DocumentException de) { HttpContext.Current.Response.Write(de.ToString()); } } } } </div>
试试其它关键字
DataTable导出
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3