2010-04-30 73 views
2

目前,我正面臨着將完整的GridView數據導出到pdf文件的乏味問題,以便用戶可以保存它。我使用C#作爲Asp.net 3.5中的語言。請引導我。如何在Asp.net和C#中將GridView數據導出爲pdf文件?

gridview只包含文本值。

在此先感謝。

+0

幾乎所有的鏈路使用iTextSharp的...是...可靠任何版權問題什麼的。除了iTextSharp之外,還有其他的方式嗎? – HotTester 2010-04-30 16:15:34

+0

iTextSharp是可靠的,但你會發現它記錄不完善。它也是開源的。 http://sourceforge.net/projects/itextsharp/ – 2010-04-30 16:17:14

+0

請嘗試使用此技巧中的方法([http://www.codeproject.com/Tips/190144/Export-Database-to-Excel-PDF-HTML-RTF -XML-等換的.aspx](http://www.codeproject.com/Tips/190144/Export-Database-to-Excel-PDF-HTML-RTF-XML-etc-for-.aspx))。這可能會有所幫助。 – 2012-02-21 07:57:21

回答

1

nFOP + XSLT + XML = pdf | doc | HTML

開源沒有成本:)

ķ

+0

感謝您的回答。請分享任何鏈接,說明其將gridview數據導出爲pdf的實現。 – HotTester 2010-05-02 17:01:09

+0

http://nfop.sourceforge.net/article.html應該給你一個關於如何使用它的想法,你需要「Microsoft Visual J#NET Redistributable Package」來運行nFOP。 K – TheOCD 2010-05-02 20:20:51

0
public override void VerifyRenderingInServerForm(Control control) 
{ 
    /* Verifies that the control is rendered */ 
} 

protected void GeneratePDF_Click(object sender, EventArgs e) 
{ 
    Response.ContentType = "application/pdf"; 
    Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf"); 
    Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    StringWriter sw = new StringWriter(); 
    HtmlTextWriter hw = new HtmlTextWriter(sw); 
    GridView1.AllowPaging = false; 
    GridView1.DataBind(); 
    GridView1.RenderControl(hw); 
    GridView1.HeaderRow.Style.Add("width", "15%"); 
    GridView1.HeaderRow.Style.Add("font-size", "10px"); 
    GridView1.Style.Add("text-decoration", "none"); 
    GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif;"); 
    GridView1.Style.Add("font-size", "8px"); 
    StringReader sr = new StringReader(sw.ToString()); 
    Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f); 
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc); 
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream); 
    pdfDoc.Open(); 
    htmlparser.Parse(sr); 
    pdfDoc.Close(); 
    Response.Write(pdfDoc); 
    Response.End(); 
}