ASP.NET Export/Report Database to Pdf |
ASP.NET Export/Report Database to Pdf ตัวอย่างการเขียน ASP.NET สร้าง Report เอกสาร PDF โดยทำการดึงข้อมูลจาก Database มาเพื่อทำเป็นรายงาน
Language Code : VB.NET || C#
ASP.NET Add Reference
Framework : 1,2,3,4
Instance NameSpace
VB.NETImports PdfSharp
C#Using PdfSharp;
AspNetPDFReport.aspx
<%@ Import Namespace="PdfSharp"%>
<%@ Import Namespace="PdfSharp.Drawing"%>
<%@ Import Namespace="PdfSharp.Pdf"%>
<%@ Import Namespace="PdfSharp.Pdf.IO"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
' Create a new PDF document
Dim DocPDF As PdfDocument = New PdfDocument
' Create an empty page
Dim objPage As PdfPage = DocPDF.AddPage
' Get an XGraphics object for drawing
Dim gfx As XGraphics = XGraphics.FromPdfPage(objPage)
' Create a font
Dim font1 As XFont = New XFont("Verdana", 15, XFontStyle.Bold)
Dim font2 As XFont = New XFont("Tahoma", 8, XFontStyle.Bold)
Dim font3 As XFont = New XFont("Tahoma", 8, 0)
' Draw the text
gfx.DrawString("My Customer", font1, XBrushes.Black, _
New XRect(0, 50, objPage.Width.Point, objPage.Height.Point), XStringFormats.TopCenter)
' Draw the text
gfx.DrawString("CustomerID", font2, XBrushes.Black, 65, 80, XStringFormats.TopLeft)
gfx.DrawString("Name", font2, XBrushes.Black, 140, 80, XStringFormats.TopLeft)
gfx.DrawString("Email", font2, XBrushes.Black, 215, 80, XStringFormats.TopLeft)
gfx.DrawString("CountryCode", font2, XBrushes.Black, 365, 80, XStringFormats.TopLeft)
gfx.DrawString("Budget", font2, XBrushes.Black, 425, 80, XStringFormats.TopLeft)
gfx.DrawString("Used", font2, XBrushes.Black, 485, 80, XStringFormats.TopLeft)
' Line
Dim pen As XPen = New XPen(XColor.FromArgb(0, 0, 0))
gfx.DrawLine(pen, New XPoint(65, 78), New XPoint(520, 78))
gfx.DrawLine(pen, New XPoint(65, 90), New XPoint(520, 90))
' Customer From Database (Start)
Dim objConn As New OleDbConnection
Dim objCmd As New OleDbCommand
Dim dtAdapter As New OleDbDataAdapter
Dim ds As New DataSet
Dim dt As DataTable
Dim strConnString,strSQL As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";Jet OLEDB:Database Password=;"
strSQL = "SELECT * FROM customer"
objConn.ConnectionString = strConnString
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds)
dt = ds.Tables(0)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
Dim intLine As Integer = 90
Dim i As Integer
For i = 0 To dt.Rows.Count - 1
gfx.DrawString(dt.Rows(i)("CustomerID"), font3, XBrushes.Black, 65, intLine, XStringFormats.TopLeft)
gfx.DrawString(dt.Rows(i)("Name"), font3, XBrushes.Black, 140, intLine, XStringFormats.TopLeft)
gfx.DrawString(dt.Rows(i)("Email"), font3, XBrushes.Black, 215, intLine, XStringFormats.TopLeft)
gfx.DrawString(dt.Rows(i)("CountryCode"), font3, XBrushes.Black, 365, intLine, XStringFormats.TopLeft)
gfx.DrawString(dt.Rows(i)("Budget"), font3, XBrushes.Black, 425, intLine, XStringFormats.TopLeft)
gfx.DrawString(dt.Rows(i)("Used"), font3, XBrushes.Black, 485, intLine, XStringFormats.TopLeft)
intLine = intLine + 10
Next
' Customer From Database (End)
' Save the document...
Dim FileName As String = "MyPDF/PdfDoc.pdf"
DocPDF.Save(Server.MapPath(FileName))
DocPDF.Close()
DocPDF = Nothing
Me.lblText.Text = "PDF Created <a href=" & FileName & ">click here</a> to view"
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - Send Mail</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label id="lblText" runat="server"></asp:Label>
</form>
</body>
</html>
Screenshot
|