尝试使用C#将数据网格视图导出为PDF

时间:2016-05-05 05:58:36

标签: c#

enter image description here

尝试使用C#

将数据网格视图导出为PDF文件

btnexport_Click 事件调用Method ExportGridToPDF();

以下是代码:

   private void btnexport_Click(object sender, EventArgs e)

    {          
      ExportGridToPDF();  
    }


   private void ExportGridToPDF()

    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=GetStudent.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        dataGridView1.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
        dataGridView1.AllowPaging = false;
        dataGridView1.DataBind();           
    } 

0 个答案:

没有答案