DataTable导出为word,excel,html,csv,pdf,.txt
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, , Font.NORMAL, new Color(, , )); // 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 = ; i < datatable.Rows.Count; i++)
{
for (int j = ; 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());
}
}
}
}
DataTable导出为word,excel,html,csv,pdf,.txt的更多相关文章
- datatable导出到Word / Excel / PDF / HTML .NET
原文发布时间为:2011-01-21 -- 来源于本人的百度文章 [由搬家工具导入] IEnumerable - DataTable Export to Word / Excel / PDF / HT ...
- Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享
Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享 在此,先分享下写此文前的经历与感受,我所有的感觉浓缩到一个字,那就是:"坑&qu ...
- word/excel/ppt 2 PDF
PHP 实现 word/excel/ppt 转换为 PDF 一般最常见的就是利用OpenOffice来转换,来看看实现的核心代码: class PDFConverter { private $com; ...
- NPOI通过DataTable导出和读取Excel
Excel导入及导出问题产生: 从接触.net到现在一直在维护一个DataTable导出到Excel的类,时不时还会维护一个导入类.以下是时不时就会出现的问题: 导出问题: 如果是asp.net,你得 ...
- .net中将DataTable导出到word、Excel、txt、htm的方法
dt:DataTable strFile:fileName strExt:type private void GridExport(DataTable dt, string strFile, stri ...
- C#将Word,Excel与Html,PDF互转
public class OfficeHelper { /// <summary> /// word转成html /// </summary> /// <param na ...
- word,excel,ppt转Pdf,Pdf转Swf,通过flexpaper+swftools实现在线预览
其实这是我好几年前的项目,现在再用这种方式我也不建议了,毕竟未来flash慢慢会淘汰,此方式也是因为目测大部分人都装了flash,才这么做的,但是页面展示效果也不好.其实还是考虑收费的控件,毕竟收费的 ...
- word,excel,ppt转pdf
第一步 需要下载jar包和jacob-1.14.3-x64.dll * <dependency> * <groupId>net.sf.jacob-project</gro ...
- c#使用aspose.cells 从datatable导出数据到excel
string json=value.Value; DataTable dt=Utils.JsonDataTableConvert.ToDataTable(json); string fileName ...
随机推荐
- php中静态方法的使用
静态方法 (1)静态方法不能访问这个类中的普通属性,因为那些属性属于一个对象,但可以访问静态属性: (2)从当前类(不是子类)中访问静态方法或属性,可以使用 self 关键字,self 指向当前类,就 ...
- 图文并茂 —— 基于Oozie调度Sqoop
利用大数据来做BI分析的时候,必不可少需要设置一些调度任务. 本篇就讲述一下如何利用hue来编辑shell操作,这里面的很多操作在其他的调度操作里面也是可以借鉴的. 如果是linux里面可以直接执行的 ...
- MySQL到底能支持多大的数据量?
MySQL是中小型网站普遍使用的数据库之一,然而,很多人并不清楚MySQL到底能支持多大的数据量,再加上某些国内CMS厂商把数据承载量的责任推给它,导致很多不了解MySQL的站长对它产生了很多误解,那 ...
- eclipse:报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
JavaWeb: 报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Bui ...
- 基于SIFT特征的全景图像拼接
基于SIFT特征的全景图像拼接 分类: image Machine learning2013-07-05 13:33 2554人阅读 评论(3) 收藏 举报 基于SIFT特征的全景图像拼接 分类: 计 ...
- Android studio3.1.3 打包jar,混淆
最近公司需要将数据进行打包提供给用户,需要我们提供数据解析的jar给用户,为了防止数据格式的泄露,需要进行混淆.这里记录一下封装jar并混淆的过程. 1.创建module 之后创建了几个需要演示混淆的 ...
- Android Studio 插件(plugins)或者intellij idea 插件(plugins)无法安装
通常这种情况出现都因为idea.properties修改了 idea.system.path=${指定路径}/system idea.plugins.path=${idea.config.path}/ ...
- 使用 .toLocaleString() 轻松实现多国语言价格数字格式化
用代码对数字进行格式化,显然不是逢三位加逗号这么简单.比如印度在数字分位符号上的处理,就堪称业界奇葩: 印度的数字读法用“拉克”(十万)和“克若尔”(千万),数字标法用不对称的数位分离,即小数点左侧首 ...
- JS中的PadLeft、PadRight,位数不足,自动补位,String扩展方法
类似C#中的 PadLeft.PadRight方法 //方法一 function FillZero(p) { return new Array(3 - (p + '').length + 1).joi ...
- 【Spark 深入学习 07】RDD编程之旅基础篇03-键值对RDD
--------------------- 本节内容: · 键值对RDD出现背景 · 键值对RDD转化操作实例 · 键值对RDD行动操作实例 · 键值对RDD数据分区 · 参考资料 --------- ...