ASP Charts/Graph (Excel.Application) |
ASP Charts/Graph ภาษา ASP การเขียนกราฟนั้นผมยอมรับว่าทำได้ยากมากก เพราะตัว ASP เองนั้นก็ไม่มี Function ที่ใช้ในการสร้างกราฟโดยตรง จะต้องอาศัย Component จากภายนอกมาติดตั้ง และเท่าที่ผมลองหาดูในส่วนของ Component ของฟรีหาไม่มีครับ ถ้าอยากจะใช้งานกราฟจริง ๆ อาจจะต้องทำการซื้อ Component จากของต่างประเทศมาใช้งานครับ แต่ถ้าจะลองใช้งานกราฟของ Excel ก็สามารถทำไดเช่นเดียวกัน โดยอาศัยการเขียน ASP เรียกใช้งาน DCOM ของ Excel.Application ทำการส่งค่าและพารามิเตอร์ตามรูปแบบของกราฟ ก็จะได้กราฟตามต้องการ
Config Excel.Application
Excel.Application Charts Syntax
<%
Server.CreateObject("Excel.Application")
Charts("ChartName").ChartType = ChartID
%>
ChartID
AspExcelCharts.asp
<% Option Explicit %>
<html>
<head>
<title>ThaiCreate.Com ASP Excel.Application Tutorial</title>
</head>
<body>
<%
Dim xlApp,xlBook,xlSheet
Dim Conn,strSQL,objRec,arrCus,intStartRows,intEndRows,i
Dim Fso,MyFile
Dim bXlsFile,FileName
FileName = "MyXls/MyChart.xls"
Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("mydatabase.mdb"),"" , ""
strSQL = "SELECT Name,Used FROM customer "
Set objRec = Server.CreateObject("ADODB.Recordset")
objRec.Open strSQL, Conn, 1,3
If Not objRec.EOF and Not objRec.BOF Then
arrCus = objRec.GetRows()
End If
intStartRows = 3
intEndRows = CInt(intStartRows)+CInt(Ubound(arrCus,2))
objRec.Close
Conn.Close
Set objRec = Nothing
Set Conn = Nothing
'*************** Start Generate Chart Excel *******************'
Set xlApp = Server.CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
xlApp.Application.Visible = False
'*** Delete Sheet (2,3) ***'
'xlBook.Worksheets(2).Select
'xlBook.Worksheets(2).Delete
'xlBook.Worksheets(2).Select
'xlBook.Worksheets(2).Delete
xlBook.Sheets.Add
xlBook.ActiveSheet.Name = "Customer"
With xlBook.Sheets("Customer").Cells(1,1)
.Value = "My Customer"
.Font.Bold = True
.Font.Name = "Tahoma"
.Font.Size = 16
End With
With xlBook.Sheets("Customer").Cells(2,1)
.Value = "Customer Name"
.Font.Name = "Tahoma"
.BORDERS.Weight = 1
.Font.Size = 10
.MergeCells = True
End With
With xlBook.Sheets("Customer").Cells(2,2)
.Value = "Used"
.BORDERS.Weight = 1
.Font.Name = "Tahoma"
.Font.Size = 10
.MergeCells = True
End With
For i = 0 To Ubound(arrCus,2)
xlBook.Sheets("Customer").Cells(intStartRows+i,1).Value = arrCus(0,i)
xlBook.Sheets("Customer").Cells(intStartRows+i,2).Value = arrCus(1,i)
xlBook.Sheets("Customer").Cells(intStartRows+i,2).NumberFormat = "$#,##0.00"
Next
'*** Creating Chart ***'
xlBook.Charts.Add
xlBook.ActiveChart.Name = "ExcelGraph"
xlBook.Charts("ExcelGraph").SetSourceData (xlBook.Sheets("Customer").Range("A"&intStartRows&":B"&intEndRows&""))
'*** Chart 3D Columns ***'
With xlBook.Charts("ExcelGraph")
.ChartType = 54
.PlotBy = 1
.HasAxis(1) = 0
End With
'*** If Already exist delete files ***'
Set Fso = CreateObject("Scripting.FileSystemObject")
If (Fso.FileExists(Server.MapPath(FileName))) Then
Set MyFile = Fso.GetFile(Server.MapPath(FileName))
MyFile.Delete
End If
xlSheet.SaveAs Server.MapPath(FileName)
xlApp.Application.Quit
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
%>
Excel Created <a href="<%=FileName%>">Click here</a> to Download.
</body>
</html>
จากรูปจะเห็นว่ากราฟถูกเขียนลงไฟล์ Excel (*.xls) ซึ่งถูกเขียนไว้คนล่ะ Sheet
AspExcelCharts2.asp ตัวอย่างนี้จะเป็นการเขียนกราฟให้อยู่ใน Sheet เดียวกัน
<% Option Explicit %>
<html>
<head>
<title>ThaiCreate.Com ASP Excel.Application Tutorial</title>
</head>
<body>
<%
Dim xlApp,xlBook,xlSheet
Dim Conn,strSQL,objRec,arrCus,intStartRows,intEndRows,i
Dim Fso,MyFile
Dim bXlsFile,FileName
FileName = "MyXls/MyChart.xls"
Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("mydatabase.mdb"),"" , ""
strSQL = "SELECT Name,Used FROM customer "
Set objRec = Server.CreateObject("ADODB.Recordset")
objRec.Open strSQL, Conn, 1,3
If Not objRec.EOF and Not objRec.BOF Then
arrCus = objRec.GetRows()
End If
intStartRows = 3
intEndRows = CInt(intStartRows)+CInt(Ubound(arrCus,2))
objRec.Close
Conn.Close
Set objRec = Nothing
Set Conn = Nothing
'*************** Start Excel.Application *******************'
Set xlApp = Server.CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
xlApp.Application.Visible = False
'*** Delete Sheet (2,3) - Sheet Default ***'
xlBook.Worksheets(2).Select
xlBook.Worksheets(2).Delete
xlBook.Worksheets(2).Select
xlBook.Worksheets(2).Delete
'*** Sheet Data Rows ***'
xlBook.Worksheets(1).Name = "MyReport"
xlBook.Worksheets(1).Select
With xlBook.ActiveSheet.Cells(1,1)
.Value = "My Customer"
.Font.Bold = True
.Font.Name = "Tahoma"
.Font.Size = 16
End With
With xlBook.ActiveSheet.Cells(2,1)
.Value = "Customer Name"
.Font.Name = "Tahoma"
.BORDERS.Weight = 1
.Font.Size = 10
.MergeCells = True
End With
With xlBook.ActiveSheet.Cells(2,2)
.Value = "Used"
.BORDERS.Weight = 1
.Font.Name = "Tahoma"
.Font.Size = 10
.MergeCells = True
End With
For i = 0 To Ubound(arrCus,2)
xlBook.ActiveSheet.Cells(intStartRows+i,1).Value = arrCus(0,i)
xlBook.ActiveSheet.Cells(intStartRows+i,2).Value = arrCus(1,i)
xlBook.ActiveSheet.Cells(intStartRows+i,2).NumberFormat = "$#,##0.00"
Next
'*** End Data Rows ***'
'*** Creating Chart ***'
xlBook.Charts.Add
xlBook.ActiveChart.ChartType = 54
xlBook.ActiveChart.SetSourceData (xlBook.Sheets("MyReport").Range("A"&intStartRows&":B"&intEndRows&""))
'xlBook.ActiveChart.SeriesCollection(1).Name = "Series1"
'xlBook.ActiveChart.SeriesCollection(2).Name = "Series1"
xlBook.ActiveChart.Location 2, "MyReport"
'*** Sheet Properties ***'
With xlBook.ActiveChart
.HasTitle = True
'.PlotBy = 1
.HasAxis(1) = 1
.ChartTitle.Text = "Customer Report"
.ChartTitle.Characters.Text = "Customer Report"
.ChartTitle.Font.Name = "Tahoma"
.ChartTitle.Font.FontStyle = "Bold"
.ChartTitle.Font.Size = 20
.ChartTitle.Font.ColorIndex = 3
.Axes(1,1).HasTitle = True
.Axes(1,1).AxisTitle.Characters.Text = "X"
.Axes(2,1).HasTitle = True
.Axes(2,1).AxisTitle.Characters.Text = "Y"
.HasDataTable = False '*** DataTable (False/True) ***'
End With
'*** Set Area ***'
xlBook.ActiveSheet.Shapes("Chart 1").IncrementLeft 20
xlBook.ActiveSheet.Shapes("Chart 1").IncrementTop -97.5
'*** Set Height & Width ***'
xlBook.ActiveSheet.Shapes("Chart 1").ScaleHeight 1.0, 0,0
xlBook.ActiveSheet.Shapes("Chart 1").ScaleWidth 1.0, 0,0
'*** If Already exist delete files ***'
Set Fso = CreateObject("Scripting.FileSystemObject")
If (Fso.FileExists(Server.MapPath(FileName))) Then
Set MyFile = Fso.GetFile(Server.MapPath(FileName))
MyFile.Delete
End If
xlSheet.SaveAs Server.MapPath(FileName)
xlApp.Application.Quit
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
%>
Excel Created <a href="<%=FileName%>">Click here</a> to Download.
</body>
</html>
|