如何在asp.net 4.0中将数据从oracle数据库绑定到pdf?
答案 0 :(得分:0)
我不知道这是否可以直接。您可以查看iTextSharp以在.NET中生成PDF文件。
答案 1 :(得分:0)
答案 2 :(得分:0)
非常感谢您的回复。我得到了答案。下面是将数据库表从Oracle数据库绑定到ASP.net中的PDF的代码
using System.Web.UI.WebControls;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Data;
public partial class generate_pdf_from_dataset : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
OracleConnection con = new OracleConnection("User id=book;Password=book;Data Source=test");
OracleDataAdapter da = new OracleDataAdapter("select * from category" , con);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
Document pdfDoc = new Document(PageSize.A4, 30, 30, 40, 25);
System.IO.MemoryStream mStream = new System.IO.MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, mStream);
int cols = dt.Columns.Count;
int rows = dt.Rows.Count;
pdfDoc.Open();
iTextSharp.text.Table pdfTable = new iTextSharp.text.Table(cols, rows);
pdfTable.BorderWidth = 1;
pdfTable.Width = 100;
pdfTable.Padding = 1;
pdfTable.Spacing = 1;
//creating table headers
for (int i = 0; i < cols; i++)
{
Cell cellCols = new Cell();
Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD);
Chunk chunkCols = new Chunk(dt.Columns[i].ColumnName, ColFont);
cellCols.Add(chunkCols);
pdfTable.AddCell(cellCols);
}
//creating table data (actual result)
for (int k = 0; k < rows; k++)
{
for (int j = 0; j < cols; j++)
{
Cell cellRows = new Cell();
Font RowFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);
Chunk chunkRows = new Chunk(dt.Rows[k][j].ToString(), RowFont);
cellRows.Add(chunkRows);
pdfTable.AddCell(cellRows);
}
}
pdfDoc.Add(pdfTable);
pdfDoc.Close();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=Report.pdf");
Response.Clear();
Response.BinaryWrite(mStream.ToArray());
Response.End();
}
}