导出为PDF没有记录

时间:2013-04-01 05:59:32

标签: c# asp.net export-to-pdf

这是来自钦奈的Arun。我正在做一个项目,将Gridview中的数据导出为PDF。一切都很好,每当我点击PDF图片时都会打开一个新的pdf文档但是没有显示任何记录。

我在Gridview中有15条记录,并且这些记录已使用表适配器显示。我在Gridview中使用了Sand and Sky Auto Formatting选项和颜色,tablecell宽度,15行完美地以PDF格式显示。

请指导我错过了什么。

谢谢。

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Export2PDF.DAL.DataSet1TableAdapters;
using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;


namespace Export2PDF
{
    public partial class _Default : System.Web.UI.Page
    {

        DataTable1TableAdapter ds1 = new DataTable1TableAdapter();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ShowGridView(object sender, EventArgs e)
        {
            Bindgrid();
        }

        public void Bindgrid()
        {
            gv1.DataSource = ds1.BatchStatusByBatches(Convert.ToDateTime(TextBox1.Text), Label1.Text);
            gv1.DataBind();
        }

        protected void gv1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gv1.PageIndex = e.NewPageIndex;
            Bindgrid();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {

        }

        protected void PDFExportButton_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=ExportToPDF.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            gv1.AllowPaging = false;
            gv1.DataSource = ds1.BatchStatusByBatches(Convert.ToDateTime(TextBox1.Text), Label1.Text);
            gv1.DataBind();
            gv1.RenderControl(hw);
            gv1.HeaderRow.Style.Add("width", "15%");
            gv1.HeaderRow.Style.Add("font-size", "10px");
            gv1.Style.Add("text-decoration", "none");
            gv1.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            gv1.Style.Add("font-size", "8px");
            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();
            Response.Clear();
        }

    }
}

1 个答案:

答案 0 :(得分:0)

尝试在表单中添加以下内容:

public override void VerifyRenderingInServerForm(Control control)
{
   // Confirms that an HtmlForm control is rendered for the
   // specified ASP.NET server control at run time.
   // No code required here.
}

来源:http://forums.asp.net/t/1047688.aspx/1