0

我想以 pdf 格式导出面板内容,但出现错误,在面板中我有表格和 Gridview。

ASPX 页面:

         <asp:Panel ID = "pnl_export" runat = "server" >
                <table>
                <tr>
                    <td>
                        <asp:Label ID="rptname" runat="server" Text=""></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lbid" runat="server"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="lbexp" runat="server" Text="Report Exported On : "></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lbdate" runat="server"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="lfrm" runat="server" Text="From : "></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="fdate" runat="server"></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lto" runat="server" Text="To : "></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="tdate" runat="server"></asp:Label>
                    </td>
                </tr>
                </table>
                <asp:GridView ID="GridView1" runat="server" Width="63%" align="Center" PageSize="20"
                    AllowPaging="true" OnRowDataBound="GridView1_RowDataBound" OnPageIndexChanging="GridView1_PageIndexChanging">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
   <PagerSettings Mode="NextPreviousFirstLast" FirstPageText="First" LastPageText="Last"
                        NextPageText="Next" PreviousPageText="Prev" Position="TopAndBottom" />

                </asp:GridView>
                </asp:Panel>

pdf导出的代码背后:

         protected void pdfExport_Click(object sender, EventArgs e)
        {
         using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
               // GridView1.AllowPaging = false;
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=AgentReport.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                pnl_export.RenderControl(hw);
                String htmlString = sw.ToString().Substring(0, sw.ToString().Length - 1); 
                StringReader sr = new StringReader(htmlString);
                Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
                pdfDoc.Open();
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

                htmlparser.Parse(sr);
                pdfDoc.Close();

                Response.Write(pdfDoc);
                Response.End();

            }

        }


    }

它不工作给我错误,

   Error : Unable to cast object of type 'iTextSharp.text.html.simpleparser.TableWrapper' to type 'iTextSharp.text.ITextElementArray'. 

HTML 内容:

        <div id="pnl_export">

    <table>
        <tr>
            <td>
                <span id="rptname">Agent Report For AgentID : </span>
            </td>
            <td>
                <span id="lbid">RSBOM0400986</span>
            </td>
        </tr>
        <tr>
            <td>
                <span id="lbexp">Report Exported On : </span>
            </td>
            <td>
                <span id="lbdate">10:01:34 AM</span>
            </td>
        </tr>
        <tr>
            <td>
                <span id="lfrm">From : </span>
            </td>
            <td>
                <span id="fdate">25/11/2014</span>
            </td>
            <td>
                <span id="lto">To : </span>
            </td>
            <td>
                <span id="tdate">28/11/2014</span>
            </td>
        </tr>
        <div>
    <table cellspacing="0" rules="all" align="Center" border="1" id="GridView1" style="width:63%;border-collapse:collapse;">
        <tr style="color:White;background-color:#5D7B9D;font-weight:bold;">
            <th scope="col">Sr.No</th><th scope="col">Sales Person</th><th scope="col">Remark</th><th scope="col">Date Visited</th>
        </tr><tr class="Textsmall" align="center" valign="middle" style="color:#333333;background-color:#F7F6F3;border-style:None;">
            <td>1</td><td>Manish Goswami-700145</td><td>fhvhvhffffffftt</td><td>26-Nov-14 5:53:49 PM</td>
        </tr><tr class="Textsmall" align="center" valign="middle" style="color:#284775;background-color:White;border-style:None;">
            <td>2</td><td>Manish Goswami-700145</td><td>remark km from my name of the way to</td><td>26-Nov-14 5:59:23 PM</td>
        </tr>
    </table>
</div>
    </table>

请帮我解决这个错误。在此先感谢。

4

1 回答 1

1

试试这个方法

protected void pdfExport_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=AgentReport.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    pnl_export.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}
于 2014-11-29T05:14:06.927 回答