DataTable exported as Word,excel,html,csv,pdf,.txt

Source: Internet
Author: User
Tags urlencode

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{/// <summary>//Export Word files/// </summary>/// <param name= "FileType" ></param>/// <param name= "FileName" ></param> Public voidExporttodoc (stringFileName, System.Web.UI.Control Control) {stringstrFileName =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 ());//////////////////////Remove CharactersHttpContext.Current.Response.End ();}/// <summary>///Export Excel File/// </summary>/// <param name= "FileType" ></param>/// <param name= "FileName" ></param> Public voidexportToExcel (stringFileName, System.Web.UI.Control Control) {stringstrFileName =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/mswordControl. 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 ("<meta http-equiv=content-type content=\ "text/html; charset=utf-8\" >"); HttpContext.Current.Response.Write (TW. ToString (). Trim ());//////////////////////Remove CharactersHttpContext.Current.Response.End (); System.Web.HttpContext.Current.Response.End ();}/// <summary>///Export HTML.................../// </summary>/// <param name= "FileName" ></param>/// <param name= "Control" ></param> Public voidExporttohtml (stringFileName, System.Web.UI.Control Control) {HttpContext.Current.Response.Clear (); HttpContext.Current.Response.Buffer=true;stringstrFileName =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 ();}/// <summary>///Export CSV/// </summary>/// <param name= "FileName" ></param>/// <param name= "Control" ></param> Public voidExporttocsv (stringFileName, DataSet DS) {stringstrFileName =System.Web.HttpUtility.UrlEncode (FileName, System.Text.Encoding.UTF8);stringdata =Exportcsv (DS);stringtemp =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>///export DataSet to CSV format/// </summary>/// <param name= "ds" >DataSet</param>/// <returns>CSV String Data</returns> Public Static stringexportcsv (DataSet ds) {stringdata ="";//data = ds. DatasetName + "\ n";foreach(DataTable TBinchds. Tables) {Data+ = tb. TableName +"\ n";//write out the column nameforeach(DataColumn columninchTB. Columns) {Data+ = column. ColumnName +",";} Data+="\ n";//Write Dataforeach(DataRow rowinchTB. Rows) {foreach(DataColumn columninchTB. Columns) {Data+ = Row[column]. ToString () +",";} Data+="\ n";} Data+="\ n";}returndata;} Public voidExportpdf (DataTable DataTable) {Try{Document Document=NewDocument (); Pdfwriter.getinstance (document,NewFileStream ("chap0101.pdf", FileMode.Create)); Document. Open (); Basefont Bfchinese= Basefont.createfont ("c:windowsfontssimsun.ttc,1", Basefont.identity_h, basefont.not_embedded); Font Fontchinese=NewFont (Bfchinese, A, Font.normal,NewColor (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(inti =0; I < DataTable. Rows.Count; i++){ for(intj =0; J < DataTable. Columns.count; J + +) {table. Addcell (NewPhrase (DataTable. ROWS[I][J]. ToString (), Fontchinese));}} Document. ADD (table);d ocument. Close ();}Catch(Documentexception de) {HttpContext.Current.Response.Write (DE. ToString ()); }}}}

DataTable exported as Word,excel,html,csv,pdf,.txt

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.