`
`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的内容,而不导出关联的母版页。
答案 0 :(得分:0)
已编辑
背后的代码:
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();
}