将选定的网格导出为PDF

时间:2019-03-16 18:03:54

标签: c# asp.net

我正在尝试将GridView数据导出为pdf,但是每当我尝试运行它时,都会出现此错误。可能是一个愚蠢的问题,但这是我第一次将数据导出到pdf中的尝试。

这是代码

编辑*对不起,我不清楚。基本上,我正在尝试此操作,但是每次尝试运行该屏幕快照时,都会收到屏幕截图中的错误

https://www.aspsnippets.com/Articles/Export-selected-checked-GridView-Rows-to-PDF-in-ASPNet.aspx

protected void Page_Load(object sender, EventArgs e)
{
    string ConnectionString = ConfigurationManager.ConnectionStrings["InvoiceID"].ConnectionString;
    using (SqlConnection con = new SqlConnection(ConnectionString))
    {
        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblClinicInvoices", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}


protected void btnPrint_Click(object sender, EventArgs e)
{
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {

            //Hide the Column containing CheckBox
            GridView1.Columns[0].Visible = false;
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    //Hide the Row if CheckBox is not checked
                    row.Visible = (row.FindControl("chkselected") as CheckBox).Checked;
                }
            }

            GridView1.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.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Write(pdfDoc);
            Response.End();
        }
    }
}

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

}

The error

0 个答案:

没有答案