如何使用c#在asp.net中创建gridviewdata的pdf [英] how to create pdf of gridviewdata in asp.net using c#
本文介绍了如何使用c#在asp.net中创建gridviewdata的pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何创建在asp .net c#中创建pdf到所有格式化我已尝试使用iTextsharp dll,但在代码行上有错误对象引用未设置为对象的实例。
gvdetails.HeaderRow.Style.Add(width,15%);。
How to Create create the pdf in asp .net c# to all formating i have try using iTextsharp dll but having error "Object reference not set to an instance of an object."on code line
"gvdetails.HeaderRow.Style.Add("width", "15%");".
推荐答案
看看这个链接:
一体化出口数据在ASP.NET [ ^ ]
使用C#(。NET 2.0)和iTextSharp创建PDF表格[ ^ ]
如何使用C#将GridView数据转换为Excel,PDF,Word文件 [ ^ ] < br $>
这可以帮到你。
Look at this Links:
All in One Export Data in ASP.NET[^]
Creating PDF Tables using C# (.NET 2.0) and iTextSharp[^]
How to convert GridView data to Excel, PDF, Word file using C#[^]
This are can help you.
使用此代码
Use This code
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
Then write the code as:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindEmpGrid();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
//It solves the error "Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server."
}
protected void BindEmpGrid()
{
SqlCommand cmd = new SqlCommand("select * from EMPLOYEE", con);
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
grEmp.DataSource = dt;
grEmp.DataBind();
}
protected void btnExportToPdf_Click(object sender, EventArgs e)
{
try
{
Response.ClearContent();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=MyPdfFile.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter strWrite = new StringWriter();
HtmlTextWriter htmWrite = new HtmlTextWriter(strWrite);
HtmlForm frm = new HtmlForm();
grEmp.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(grEmp);
frm.RenderControl(htmWrite);
StringReader sr = new StringReader(strWrite.ToString());
Document pdfDoc = new Document(PageSize.A4, 8f, 8f, 8f, 2f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.Flush();
Response.End();
}
catch (Exception ex) { }
}
谢谢&注意
Sham:)
Thanks & Regard
Sham :)
这篇关于如何使用c#在asp.net中创建gridviewdata的pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文