2011-05-19 35 views
0

如何將數據從oracle數據庫綁定到pdf在asp.net 4.0中?如何從oracle直接將表綁定到pdf

+0

謝謝您reply.Actually我已經使用的iText從表中數據綁定到pdf.first我已經綁定的數據爲在ASP.net從Oracle數據庫綁定數據庫表到PDF的代碼網格視圖,然後pdf.It工作。但直接綁定是複雜的,我知道。但我必須這樣做。請任何一個幫助。提前感謝。 – srk 2011-05-19 07:18:35

回答

0

我不知道這是否可以直接。您可以查看iTextSharp以在.NET中生成PDF文件。

0

您是否嘗試過使用PL/PDF?沒有親自使用它,但它是我知道的創建/填充PDF的唯一直接方法(除非Apex有一些插件)

0

非常感謝您的回覆。我得到了答案。下面是

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(); 
    } 
}