(C#) ASP.NET Export/Report Database to Pdf |
(C#) 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="C#" Debug="true" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
// Create a new PDF document
PdfDocument DocPDF = new PdfDocument();
// Create an empty page
PdfPage objPage = DocPDF.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(objPage);
// Create a font
XFont font1 = new XFont("Verdana", 15, XFontStyle.Bold);
XFont font2 = new XFont("Tahoma", 8, XFontStyle.Bold);
XFont font3 = 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
XPen pen = 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)
OleDbConnection objConn = new OleDbConnection();
OleDbCommand objCmd = new OleDbCommand();
OleDbDataAdapter dtAdapter = new OleDbDataAdapter();
DataSet ds = new DataSet();
DataTable dt;
String strConnString,strSQL;
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;
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(ds);
dt = ds.Tables[0];
dtAdapter = null;
objConn.Close();
objConn = null;
int intLine = 90;
int i;
for(i = 0;i <= dt.Rows.Count - 1;i++)
{
gfx.DrawString(dt.Rows[i]["CustomerID"].ToString(), font3, XBrushes.Black, 65, intLine, XStringFormats.TopLeft);
gfx.DrawString(dt.Rows[i]["Name"].ToString(), font3, XBrushes.Black, 140, intLine, XStringFormats.TopLeft);
gfx.DrawString(dt.Rows[i]["Email"].ToString(), font3, XBrushes.Black, 215, intLine, XStringFormats.TopLeft);
gfx.DrawString(dt.Rows[i]["CountryCode"].ToString(), font3, XBrushes.Black, 365, intLine, XStringFormats.TopLeft);
gfx.DrawString(dt.Rows[i]["Budget"].ToString(), font3, XBrushes.Black, 425, intLine, XStringFormats.TopLeft);
gfx.DrawString(dt.Rows[i]["Used"].ToString(), font3, XBrushes.Black, 485, intLine, XStringFormats.TopLeft);
intLine = intLine + 10;
}
// Customer From Database (End)
// Save the document...
String FileName = "MyPDF/PdfDoc.pdf";
DocPDF.Save(Server.MapPath(FileName));
DocPDF.Close();
DocPDF = null;
this.lblText.Text = "PDF Created <a href=" + FileName + ">click here</a> to view";
}
</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
![](/images/adv.jpg)
|