繁体   English   中英

如何仅导出gridview内容而不导出整个页面(pdf)?

[英]How to export only gridview content but not the whole page in pdf?

`

 `I'm displaying the PDF on clicking the Export button, but after exporting what I get is the whole page. There is no other content in the aspx page but it is a web page using master page. So the contents from the master page are also being downloaded which is unwanted. 

Aspx代码

     <asp:GridView ID="GVAppRec" runat="server" AllowPaging="false" CssClass="center" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" AutoGenerateColumns="False">
                        <Columns>                   
                            <asp:TemplateField HeaderText="ReceivalID">                        
                                <ItemTemplate >
                                    <asp:Label ID="LblRecId" runat="server" Text='<% # Eval("ReceivalId") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="ApplicantID">                        
                                <ItemTemplate >
                                    <asp:Label ID="LblApId" runat="server" Text='<% # Eval("ApplicantId") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="FirstName">                        
                                <ItemTemplate >
                                    <asp:Label ID="LblFname" runat="server" Text='<% # Eval("Fname") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="LastName">                        
                                <ItemTemplate >
                                    <asp:Label ID="LblLname" runat="server" Text='<% # Eval("Lname") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="PostID">                        
                                <ItemTemplate >
                                    <asp:Label ID="LblPsId" runat="server" Text='<% # Eval("PostId") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>                   
                            <asp:TemplateField HeaderText="Status">                        
                                <ItemTemplate >
                                    <asp:CheckBox ID="CkSts" OnCheckedChanged="CheckBox1_CheckedChanged" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>                                        
                        </Columns>               
                    </asp:GridView> 

背后的代码

    PdfPTable pdfTable = new PdfPTable(GVAppRec.HeaderRow.Cells.Count);
    foreach (TableCell headerCell in GVAppRec.HeaderRow.Cells)
    {
        Font f = new Font();
        f.Color = new BaseColor(GVAppRec.HeaderStyle.ForeColor);

        PdfPCell pdfdCell = new PdfPCell(new Phrase(headerCell.Text, f));
        pdfdCell.BackgroundColor = new BaseColor(GVAppRec.HeaderStyle.BackColor);
        pdfTable.AddCell(pdfdCell);
    }

    foreach (GridViewRow gvr in GVAppRec.Rows)
    {
       foreach (TableCell tc in gvr.Cells)
       {
           Font f = new Font();
           f.Color = new BaseColor(GVAppRec.RowStyle.ForeColor);
           PdfPCell pdfdCell = new PdfPCell(new Phrase(tc.Text));
           pdfdCell.BackgroundColor = new BaseColor(GVAppRec.RowStyle.BackColor);
           pdfTable.AddCell(pdfdCell);
       }
    }

    Document doc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
    PdfWriter.GetInstance(doc, Response.OutputStream);
    doc.Open();
    doc.Add(pdfTable);
    doc.Close();

    Response.ContentType = "application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment;filename=VacancyReceived.pdf");
    Response.Write(doc);
    Response.Flush();
    Response.End();

我希望仅导出gridview的内容,而不导出关联的母版页。

已编辑

背后的代码:

private void ExportGridToPDF()  
{  

    Response.ContentType = "application/pdf";  
    Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");  
    Response.Cache.SetCacheability(HttpCacheability.NoCache);  
    StringWriter sw = new StringWriter();  
    HtmlTextWriter hw = new HtmlTextWriter(sw);  
    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();  
    GridView1.AllowPaging = true;  
    GridView1.DataBind();  
}  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM