本文介绍了在ASP.Net中将Gridview数据导出为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=Example.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); gridmeasuredetails.AllowPaging = false; gridmeasuredetails.DataBind(); //BindGrid(); gridmeasuredetails.RenderControl(hw); gridmeasuredetails.HeaderRow.Style.Add("width", "25%"); gridmeasuredetails.HeaderRow.Style.Add("font-size", "11px"); gridmeasuredetails.Style.Add("text-decoration", "none"); gridmeasuredetails.Style.Add("font-family", "Arial, Helvetica, sans-serif;"); gridmeasuredetails.Style.Add("font-size", "9px"); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); i我使用此代码将数据导出为pdf格式但是我得到了该文档没有页面如何解决此问题c# i am using this code exporting to the data into pdf format but i got The document has no pages how to solve this issue in c#推荐答案 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SendingMail.WebForm1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title></head><body> <form id="form1" runat="server"> <asp:gridview ID="gvdetails" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="VendorId" HeaderText="VendorId" SortExpression="VendorId" /> <asp:BoundField DataField="IncomeDay" HeaderText="IncomeDay" SortExpression="IncomeDay" /> <asp:BoundField DataField="IncomeAmount" HeaderText="IncomeAmount" SortExpression="IncomeAmount" /> </Columns> </asp:gridview> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<% on.cs页面: on.cs page :using System;using System.Web;using System.Web.UI;using iTextSharp.text;//need to download .dllusing iTextSharp.text.pdf;using iTextSharp.text.html.simpleparser;using System.IO;namespace SendingMail{ public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public override void VerifyRenderingInServerForm(Control control) { /* Verifies that the control is rendered */ } protected void Button1_Click(object sender, EventArgs e) { Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); gvdetails.DataBind(); gvdetails.RenderControl(hw); gvdetails.HeaderRow.Style.Add("width", "15%"); gvdetails.HeaderRow.Style.Add("font-size", "10px"); gvdetails.Style.Add("text-decoration", "none"); gvdetails.Style.Add("font-family", "Arial, Helvetica, sans-serif;"); gvdetails.Style.Add("font-size", "8px"); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); } }}<pre lang="c#"> Jmd: - )Jmd:-) 这篇关于在ASP.Net中将Gridview数据导出为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-15 20:17