0
我試圖將GridView
導出爲PDF。它給人的錯誤:ITextSharp將GridView導出爲PDF時出錯
Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'.
它引發錯誤這裏
htmlparser.Parse(sr);
我試圖將GridView
導出爲PDF。它給人的錯誤:ITextSharp將GridView導出爲PDF時出錯
Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'.
它引發錯誤這裏
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();