如何在Asp.net和C#中将GridView数据导出为pdf文件?

时间:2010-04-30 16:05:54

标签: c# .net asp.net gridview

目前,我面临着将完整的GridView数据导出到pdf文件的繁琐问题,以便用户可以保存它。我使用C#作为Asp.net 3.5中的语言。请指导我。

gridview仅包含文本值。

提前致谢。

3 个答案:

答案 0 :(得分:3)

答案 1 :(得分:1)

nFOP + XSLT + XML = pdf | doc | HTML

开源免费:)

ķ

答案 2 :(得分: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();   
}