web 2.0

How to use ASP & Charts/Graph - Create Charts and Send Email Attachment

How to use ASP & Charts/Graph - Create Charts and Send Email Attachment This is learn/tutorial asp developers how to using ASP script Create Chart/Graph - Generate Chart and Send Email Attachment

ShotDev Focus:
- ASP & Create Chart/Graph - Generate Chart and Send Email Attachment

Example

asp_chart_mail.asp


<% OptionĀ  Explicit %>
<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<%
Dim xlApp,xlBook,xlSheet
Dim Conn,strSQL,objRec,arrCus,intStartRows,intEndRows,i
Dim Fso,MyFile,objRange,colCharts,objChart
Dim bXlsFile,FileName,Ext,ExlName

'*** File Name Gif,Jpeg,... ***'
FileName = "MyXls/MyChart.Gif"
Ext = "Gif"

'*** Excel Name ***'
ExlName = "MyXls/MyChart.xls"

Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("db/mydatabase.mdb"),"" , ""
strSQL = "SELECT Name,Budget,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 = 2
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 = "Customer Name"
.Font.Name = "Tahoma"
.BORDERS.Weight = 1
.Font.Size = 10
.MergeCells = True
End With

With xlBook.ActiveSheet.Cells(1,2)
.Value = "Budget"
.BORDERS.Weight = 1
.Font.Name = "Tahoma"
.Font.Size = 10
.MergeCells = True
End With

With xlBook.ActiveSheet.Cells(1,3)
.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"
xlBook.ActiveSheet.Cells(intStartRows+i,3).Value = arrCus(2,i)
xlBook.ActiveSheet.Cells(intStartRows+i,3).NumberFormat = "$#,##0.00"
Next
'*** End Data Rows ***'

Set objRange = xlBook.Sheets("MyReport").UsedRange
objRange.Select

'*** Creating Chart ***'
xlBook.Charts.Add
'xlBook.ActiveChart.SeriesCollection(1).Name = "Series1"
'xlBook.ActiveChart.SeriesCollection(2).Name = "Series1"
xlBook.ActiveChart.Location 2, "MyReport"

'*** Chart Properties ***'
With xlBook.ActiveChart
.ChartType = 98
.HasLegend = True
.HasTitle = 1
.ChartTitle.Text = "Customer Report"
End With

'*** Legend Font ***'
With xlBook.ActiveChart.Legend
.Font.Name = "Arial"
.Font.Size = 5
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 2.0, 0,0
xlBook.ActiveSheet.Shapes("Chart 1").ScaleWidth 1.5, 0,0

'*** If Already exist delete files ***'
Set Fso = CreateObject("Scripting.FileSystemObject")

'*** Save To Gif,Jpeg ***'
If (Fso.FileExists(Server.MapPath(FileName))) Then
Set MyFile = Fso.GetFile(Server.MapPath(FileName))
MyFile.Delete
End If
'xlApp.ActiveChart.Export "C:\Inetpub\wwwroot\myasp\MyXls\MyChart.Gif","Gif"
xlApp.ActiveChart.Export Server.MapPath(FileName),Ext

'*** Save Excel ***'
If (Fso.FileExists(Server.MapPath(ExlName))) Then
Set MyFile = Fso.GetFile(Server.MapPath(ExlName))
MyFile.Delete
End If
xlSheet.SaveAs Server.MapPath(ExlName)

xlApp.Application.Quit

Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing

'**************** Send Email ******************'

Dim myMail,HTML,strMsg

Set myMail = Server.CreateObject("CDONTS.NewMail")

'*** Attach Jpg ***'
If Trim(FileName) <> "" Then
myMail.AttachFile Server.MapPath(FileName)
End If

'*** Attach Jpg ***'
If Trim(ExlName) <> "" Then
myMail.AttachFile Server.MapPath(ExlName)
End If

myMail.From = "Webmaster <webmaster@thaicreate.com>"
myMail.Value("Reply-To") = "thaicreate@hotmail.com"
myMail.To = "member@thaicreate.com"
myMail.Subject = "My Charts"
myMail.MailFormat = 0
myMail.BodyFormat = 0
myMail.Body = "Charts/Graph Report"

myMail.Send

Set myMail = Nothing

'*************** End Send Email ***************'

Response.write"Charts Created and Email Sending."
%>
</body>
</html>

Create a asp file and save to path root-path/myasp/

Run
http://localhost/myasp/asp_chart_mail.asp

Screenshot

.
.
.
Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (1 votes, average: 1.00 out of 10)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.