这是来自钦奈的阿伦。我正在做一个将数据从 Gridview 导出到 PDF 的项目。一切都很好,每当我单击 PDF 图像时都会打开一个新的 pdf 文档,但其中没有显示任何记录。
我在 Gridview 中有 15 条记录,这些记录已使用表适配器显示。我在 Gridview 中使用了沙色和天空自动格式化选项,颜色、表格单元格宽度、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();
}
}
}