简体   繁体   English

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

[英]How to export GridView data to pdf file in Asp.net and C#?

Currently I am facing the tedious problem of exporting complete GridView data to the pdf file so that the user can save it. 当前,我面临着将完整的GridView数据导出到pdf文件的繁琐问题,以便用户可以保存它。 I am using C# as the language in Asp.net 3.5. 我正在使用C#作为Asp.net 3.5中的语言。 Kindly guide me. 请指导我。

The gridview contains only text values. gridview仅包含文本值。

Thanks in advance. 提前致谢。

nFOP + XSLT + XML = pdf | nFOP + XSLT + XML = pdf | doc | doc | HTML HTML

open source no cost :) 开源免费:)

K ķ

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();   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM