将Gridview导入pdf

时间:2016-06-11 15:09:25

标签: c# asp.net pdf gridview

我需要将数据从asp.net中的Grid视图导入到pdf文件中 这是我的Gridview

<asp:GridView ID="gvValues" runat="server" Width="100%" AllowPaging="True" PagerSettings-Mode="NumericFirstLast"
    AutoGenerateColumns="False" CellPadding="0" PageSize="15" ItemType="Tayf.Models.Product" CssClass="table-striped table-condensed table table-bordered table-hover"
    OnRowDataBound="gvValues_RowDataBound" OnPageIndexChanging="gvValues_PageIndexChanging" meta:resourcekey="gvValuesResource1" EmptyDataText="No Products in your Pos">
        <EmptyDataRowStyle Font-Bold="True" Font-Size="16pt" ForeColor="Red" />
           <RowStyle Wrap="true" />
               <Columns>
                    <asp:TemplateField HeaderText="#">
                       <ItemTemplate><%# gvValues.PageSize*gvValues.PageIndex+ Container.DisplayIndex+1  %></ItemTemplate>
                     </asp:TemplateField>
                      <asp:BoundField DataField="Barcode" ReadOnly="true" HeaderText="Barcode" meta:resourcekey="BoundFieldResource1" />
                     <asp:BoundField DataField="Description" ReadOnly="true" HeaderText="Item Name" meta:resourcekey="BoundFieldResource2" />
                      <asp:BoundField DataField="Price" DataFormatString="{0:#.00}" ReadOnly="true" HeaderText="Price" meta:resourcekey="BoundFieldResource3" />
                      <asp:TemplateField HeaderText="Notes">
                      <ItemTemplate><%# Item==null?"":  Item.PosInvoiceDetail == null || Item.PosInvoiceDetail.Count<1?"": "Invoice ID:" +  Item.PosInvoiceDetail.First().PosInvoiceMasterID.ToString()  %></ItemTemplate>
                      </asp:TemplateField>
                </Columns>

</asp:GridView>

和此按钮

<asp:Button  ID="importData" runat="server" CssClass="btn-default" Text="Import To PDF" OnClick="importData_Click"></asp:Button>

这是我的代码

 protected void importData_Click(object sender, EventArgs e)
        {
            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);
            gvValues.AllowPaging = false;
            gvValues.DataBind();
            gvValues.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(); 
        }

但我有一个错误

其中说

System.Web.dll中发生了'System.Web.HttpException'类型的异常,但未在用户代码中处理

附加信息:控件'GridView'类型的'MainContent_gvValues'必须放在带有runat = server的表单标签内。

1 个答案:

答案 0 :(得分:0)

  • 当尝试将GridView控件导出为Word,Excel,PDF,CSV或任何其他格式时,会发生此异常。在这里.net编译器认为控件没有添加到表单并呈现

解决这个问题的方法就是在CS中编写下面的代码。文件

   public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified AS.net
           server control at run time. */

    }