博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DataTable导出为word,excel,html,csv,pdf,.txt
阅读量:4447 次
发布时间:2019-06-07

本文共 6210 字,大约阅读时间需要 20 分钟。

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());
}
}
}
}

转载于:https://www.cnblogs.com/xiachufeng/archive/2011/11/11/2245916.html

你可能感兴趣的文章
conversation with super KDL
查看>>
3. Git与TortoiseGit基本操作
查看>>
正則表達式匹配号码
查看>>
Codeforces Beta Round #10 B. Cinema Cashier (树状数组)
查看>>
Zookeeper zkui-zookeeper图形化管理工具
查看>>
线段树
查看>>
LLVM提议向C语言中加入模块机制
查看>>
免费学习视频
查看>>
Winodws10 &system进程占用磁盘100%
查看>>
css样式优先级
查看>>
遇见未知的自己
查看>>
js中return;、return true、return false;区别
查看>>
关于list的一些作业
查看>>
bzoj 2818: Gcd
查看>>
bzoj千题计划316:bzoj3173: [Tjoi2013]最长上升子序列(二分+树状数组)
查看>>
JDK1.8之后匿名内部类访问方法中的局部变量不用加final修饰
查看>>
九度oj题目1521:二叉树的镜像
查看>>
java运行时内存分类
查看>>
为什么说 Git 比 SVN 更好
查看>>
CSS的定位和浮动
查看>>