返回首页

DGKumar

回答

评论会员:kannan.gandhi 时间:2012/02/04
喜库马尔,
通过使用itextsharp.dll,我们可以在GridView转换成PDF。下面的代码将帮助你。在此之前,你必须添加itextsharp.dll
{A}
using System;

using System.Data;

using System.Configuration;

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 iTextSharp.text;

using iTextSharp.text.pdf;

using iTextSharp.text.html;

using System.IO;

using System.Collections;

using System.Net;

 

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

 

}

protected void btnExport_Click(object sender, EventArgs e)

{

HtmlForm form = new HtmlForm();

form.Controls.Add(GridView1);

StringWriter sw = new StringWriter();

HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);

form.Controls[0].RenderControl(hTextWriter);

string html = sw.ToString();

Document Doc = new Document();

 

//PdfWriter.GetInstance

//(Doc, new FileStream(Request.PhysicalApplicationPath 

//+ "\\AmitJain.pdf", FileMode.Create));



PdfWriter.GetInstance

(Doc, new FileStream(Environment.GetFolderPath

(Environment.SpecialFolder.Desktop)

+ "\\AmitJain.pdf", FileMode.Create));

Doc.Open();

 

Chunk c = new Chunk

("Export GridView to PDF Using iTextSharp \n",

FontFactory.GetFont("Verdana", 15));

Paragraph p = new Paragraph();

p.Alignment = Element.ALIGN_CENTER;

p.Add(c);

Chunk chunk1 = new Chunk

("By Amit Jain, amit_jain_online@yahoo.com \n",

FontFactory.GetFont("Verdana", 8));

Paragraph p1 = new Paragraph();

p1.Alignment = Element.ALIGN_RIGHT;

p1.Add(chunk1);

 

Doc.Add(p);

Doc.Add(p1);

 

System.Xml.XmlTextReader xmlReader =

new System.Xml.XmlTextReader(new StringReader(html));

HtmlParser.Parse(Doc, xmlReader);

 

Doc.Close();

string Path = Environment.GetFolderPath

(Environment.SpecialFolder.Desktop)

+ "\\AmitJain.pdf";

 



ShowPdf(Path);

 



}

 

private void ShowPdf(string strS)

{

Response.ClearContent();

Response.ClearHeaders();

Response.ContentType = "application/pdf";

Response.AddHeader

("Content-Disposition","attachment; filename=" + strS);

Response.TransmitFile(strS);

Response.End();

//Response.WriteFile(strS);

Response.Flush();

Response.Clear();

 

}

 

}

感谢
卡纳安
www.steadfastglobal.com
评论会员:prijeshyyn 时间:2012/02/04
Use SSRS报告,而不是电网。所以,我们导出到PDF格式的Excel,Word,XML等
评论会员:游客 时间:2012/02/04
|SAKryukov:有没有"转换"。(这是一种被广泛滥用,长期在CodeProject上这里。如何感染的探究?)这种做法是错误的原则。不要建立基于UI上的文件,创建以数据为基础的文件。你需要有一个单独的数据层。你的UI应绑定到数据。即使你不使用正式的数据绑定,方法应遵循模式的数据绑定:你从数据层填充UI,用户的数据层的操作,会导致在数据层的数据修改,每次修改后所有的用户界面始终无效/更新。这将缓解代文件的问题。他们将创建我们的数据层的当前数据。对于PDFAPI,尝试使用iTextSharp。请参阅:{A2}{A3的}{A4}我建议你​​学习和分析以下({A5的}])建筑模式的适用性:MVVM的mdash;模式观看模型,{A6的},MVCmdash;模型-视图-控制器,{A7的}])MVA的mdash;模型-视图-适配器,{A8的},MVPmdash;模型-视图-演示,{A9的}]支付这些架构的动机的关注。如果你了解它,你就能够创造出更好的设计思路。mdash;SA