DataTable은 word, excel, html, csv, pdf로 내보냅니다.txt
20510 단어 Datatable
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 + "
";
foreach (DataTable tb in ds.Tables)
{
data += tb.TableName + "
";
//
foreach (DataColumn column in tb.Columns)
{
data += column.ColumnName + ",";
}
data += "
";
//
foreach (DataRow row in tb.Rows)
{
foreach (DataColumn column in tb.Columns)
{
data += row[column].ToString() + ",";
}
data += "
";
}
data += "
";
}
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());
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
yui--datatable 행 추가 형식텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.