C# 将GridView导出为PDF时发生ITextSharp错误

C# 将GridView导出为PDF时发生ITextSharp错误,c#,gridview,itextsharp,export-to-pdf,C#,Gridview,Itextsharp,Export To Pdf,我尝试将GridView导出为PDF。它给出了错误: 无法将类型为“iTextSharp.text.html.simpleparser.CellWrapper”的对象强制转换为类型为“iTextSharp.text.paragration” 它在这里抛出错误 htmlparser.Parse(sr); 在数据绑定和解析之前,应该禁用排序和分页 代码如下: Response.ContentType = "application/pdf"; Response.AddHeader("content

我尝试将
GridView
导出为PDF。它给出了错误:

无法将类型为“iTextSharp.text.html.simpleparser.CellWrapper”的对象强制转换为类型为“iTextSharp.text.paragration”

它在这里抛出错误

htmlparser.Parse(sr);

在数据绑定和解析之前,应该禁用排序和分页

代码如下:

Response.ContentType = "application/pdf";

Response.AddHeader("content-disposition","attachment;filename=GridViewExport.pdf");

Response.Cache.SetCacheability(HttpCacheability.NoCache);

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);

GridView1.AllowPaging = false;     <----

GridView1.AllowSorting = false;    <----

GridView1.DataBind();

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.Write(pdfDoc);

Response.End(); 
Response.ContentType=“application/pdf”;
AddHeader(“内容处置”、“附件;文件名=GridViewExport.pdf”);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw=新的StringWriter();
HtmlTextWriter hw=新的HtmlTextWriter(sw);
GridView1.AllowPaging=false;