java导出word datatable_DataTable导出为word,excel,html,csv,pdf,.txt

usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.Data.SqlClient;usingSystem.Collections.Generic;//using iTextSharp.text;//using iTextSharp.text.pdf;

usingSystem.IO;usingSystem.Text;//using iTextSharp.text.html;

usingSystem.Xml;usingiTextSharp;usingiTextSharp.text;usingiTextSharp.text.pdf;usingSystem.Reflection;namespacezjf.Utility

{public classPrint

{///

//导出word文件

///

///

///

public void ExportToDoc(stringFileName, 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= newSystem.IO.StringWriter();

HtmlTextWriter hTw= newHtmlTextWriter(swOut);

control.RenderControl(hTw);

HttpContext.Current.Response.Write(swOut.ToString());//去除字符

HttpContext.Current.Response.End();

}///

///导出EXCEl文件///

///

///

public void ExportToExcel(stringFileName, 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= newSystem.IO.StringWriter();

System.Web.UI.HtmlTextWriter hw= newSystem.Web.UI.HtmlTextWriter(tw);

control.RenderControl(hw);

HttpContext.Current.Response.Write("");

HttpContext.Current.Response.Write(tw.ToString().Trim());//去除字符

HttpContext.Current.Response.End();

System.Web.HttpContext.Current.Response.End();

}///

///导出html

...................///

///

///

public void ExportTohtml(stringFileName, 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= newSystem.IO.StringWriter();

System.Web.UI.HtmlTextWriter oHtmlTextWriter= newSystem.Web.UI.HtmlTextWriter(oStringWriter);

StringWriter tw= newStringWriter();

HtmlTextWriter hw= newHtmlTextWriter(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();

}///

///导出CSV///

///

///

public void ExportTocsv(stringFileName, 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();

}///

///将DataSet导出成CSV格式///

/// DataSet

/// CSV字符串数据

public static stringExportCSV(DataSet ds)

{string data = "";//data = ds.DataSetName + "\n";

foreach (DataTable tb inds.Tables)

{

data+= tb.TableName + "\n";//写出列名

foreach (DataColumn column intb.Columns)

{

data+= column.ColumnName + ",";

}

data+= "\n";//写出数据

foreach (DataRow row intb.Rows)

{foreach (DataColumn column intb.Columns)

{

data+= row[column].ToString() + ",";

}

data+= "\n";

}

data+= "\n";

}returndata;

}public voidExportPDF(DataTable datatable)

{try{

Document document= newDocument();

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 = newPdfPTable(datatable.Columns.Count);for (int i = 0; i < datatable.Rows.Count; i++)

{for (int j = 0; j < datatable.Columns.Count; j++)

{

table.AddCell(newPhrase(datatable.Rows[i][j].ToString(), fontChinese));

}

}

document.Add(table);

document.Close();

}catch(DocumentException de)

{

HttpContext.Current.Response.Write(de.ToString());

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值