如何在浏览器中将数据从数据库显示为pdf [英] How to display data from database as pdf in browser

查看:71
本文介绍了如何在浏览器中将数据从数据库显示为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我的代码正在用于在文件中打开pdf格式,但我想在浏览器中以pdf格式从数据库中检索数据

下面是代码任何一个helpme或给我的例子

使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Web.UI.WebControls.WebParts;

使用System.Web.UI.HtmlControls;

使用System.Data.SqlClient;

使用System.IO;

使用iTextSharp.text;

使用iTextSharp.text.pdf;

使用iTextSharp.text.html;

使用iTextSharp.text.html .simpleparser;

使用System.Text;

使用System.Data;

使用System.Data.SqlClient;



public partial class _Default:System.Web.UI.Page

{

protected void Page_Load(object sender,EventArgs e)

{

s tring cs =Data Source = HOME; Initial Catalog = Registration; Integrated Security = True;

SqlConnection con = new SqlConnection(cs);

SqlCommand cmd = new SqlCommand (select * from employeep,con);

DataTable dt = GetData(cmd);

GridView1.DataSource = dt;

GridView1 .DataBind();



}

私有DataTable GetData(SqlCommand cmd)

{

DataTable dt = new DataTable();

String strConnString =Data Source = HOME; Initial Catalog = Registration; Integrated Security = True;

SqlConnection con = new SqlConnection(strConnString);

SqlDataAdapter sda = new SqlDataAdapter();

cmd.CommandType = CommandType.Text;

cmd.Connection = con;

尝试

{

con.Open();

sda.SelectCommand = cmd;

sda.Fill(dt);

返回dt;

}

catch(Exception ex)

{

抛出前;

}

终于

{

con.Close();

sda.Dispose();

con。 Dispose();

}

}





public override void VerifyRenderingInServerForm (控制控制)

{

/ *验证控件是否呈现* /

}

protected void OnPaging(对象发送者,GridViewPageEventArgs e)

{

GridView1.PageIndex = e.NewPageIndex;

GridView1.DataBind();

}



void btnExportPDF_Click(object sender,EventArgs e)

{

Response.ContentType =application / pdf;

Response.AddHeader(content-disposition,attachment; filename = GridViewExport.pdf);

响应。 Cache.SetCacheability(HttpCacheability.NoCache);

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);

GridView1.AllowPaging = false;

GridView1.DataBind();

GridView1.RenderControl(hw);

StringReader sr = new StringReader(sw.ToString());

文件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.Cache.SetCacheability(HttpCacheability.NoCache);

Response.End();

}

actually my code is working for opening pdf format in file but i want retrieved data from databse in pdf format in browser
below is the code any one helpme or give me examples
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Text;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string cs = "Data Source=HOME;Initial Catalog=Registration;Integrated Security=True";
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("select * from employeep",con);
DataTable dt = GetData(cmd);
GridView1.DataSource = dt;
GridView1.DataBind();

}
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
String strConnString = "Data Source=HOME;Initial Catalog=Registration;Integrated Security=True";
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}


public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}

void btnExportPDF_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
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.Cache.SetCacheability(HttpCacheability.NoCache);
Response.End();
}

推荐答案

您已完成绝大部分工作。下一步是在btnExportPDF_Click方法中遍历DataTable中的数据,并将元素添加到PDF文档中。这为您要做的事情提供了一个很好的例子:



http://stackoverflow.com/questions/24361141/how-to-export-grid-view-data-to-pdf-using-itext -sharp [ ^ ]



或者这个:



< a href =http://www.c-sharpcorner.com/UploadFile/raj1979/export-grid-view-data-to-pdf-using-itextsharp/> http://www.c-sharpcorner.com/ UploadFile / raj1979 / export-grid-view-data-to-pdf-using-itextsharp / [ ^ ]
You have already completed the vast majority of your work. The next step is to loop through the data in your DataTable in the btnExportPDF_Click method and add elements to your PDF document. This provides a good example of what you are trying to do:

http://stackoverflow.com/questions/24361141/how-to-export-grid-view-data-to-pdf-using-itext-sharp[^]

or this:

http://www.c-sharpcorner.com/UploadFile/raj1979/export-grid-view-data-to-pdf-using-itextsharp/[^]


这篇关于如何在浏览器中将数据从数据库显示为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆