目前,我正面臨着將完整的GridView數據導出到pdf文件的乏味問題,以便用戶可以保存它。我使用C#作爲Asp.net 3.5中的語言。請引導我。如何在Asp.net和C#中將GridView數據導出爲pdf文件?
gridview只包含文本值。
在此先感謝。
目前,我正面臨着將完整的GridView數據導出到pdf文件的乏味問題,以便用戶可以保存它。我使用C#作爲Asp.net 3.5中的語言。請引導我。如何在Asp.net和C#中將GridView數據導出爲pdf文件?
gridview只包含文本值。
在此先感謝。
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();
}
幾乎所有的鏈路使用iTextSharp的...是...可靠任何版權問題什麼的。除了iTextSharp之外,還有其他的方式嗎? – HotTester 2010-04-30 16:15:34
iTextSharp是可靠的,但你會發現它記錄不完善。它也是開源的。 http://sourceforge.net/projects/itextsharp/ – 2010-04-30 16:17:14
請嘗試使用此技巧中的方法([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