001.
<%@ Import
Namespace
=
"System.Data"
%>
002.
<%@ Import
Namespace
=
"System.Data.OleDb"
%>
003.
<%@ Import
Namespace
=
"Excel"
%>
004.
<%@ Import
Namespace
=
"System.IO"
%>
005.
<%@ Page Language=
"VB"
%>
006.
<script runat=
"server"
>
007.
Sub
Page_Load(sender
As
Object
, e
As
EventArgs)
008.
Dim
objConn
As
New
OleDbConnection
009.
Dim
dtAdapter
As
OleDbDataAdapter
010.
Dim
dt
As
New
System.Data.DataTable
011.
012.
Dim
strConnString
As
String
013.
strConnString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
& Server.MapPath(
"database/mydatabase.mdb"
) &
";"
014.
objConn =
New
OleDbConnection(strConnString)
015.
objConn.Open()
016.
017.
Dim
strSQL
As
String
018.
strSQL =
"SELECT * FROM customer"
019.
020.
021.
dtAdapter =
New
OleDbDataAdapter(strSQL, objConn)
022.
dtAdapter.Fill(dt)
023.
024.
025.
Dim
FileName
As
String
=
"MyXls/MyExcel.xls"
026.
027.
028.
Dim
xlApp
As
New
Excel.Application
029.
Dim
xlSheet
As
Excel.Worksheet
030.
Dim
xlBook
As
Excel.Workbook
031.
Dim
i, intStartRows, intEndRows
As
Integer
032.
033.
xlBook = xlApp.Workbooks.Add()
034.
xlSheet = xlBook.Worksheets(1)
035.
036.
xlApp.Application.Visible =
False
037.
038.
039.
xlBook.Worksheets(2).
Select
()
040.
xlBook.Worksheets(2).Delete()
041.
xlBook.Worksheets(2).
Select
()
042.
xlBook.Worksheets(2).Delete()
043.
044.
045.
xlBook.Worksheets(1).Name =
"MyReport"
046.
xlBook.Worksheets(1).
Select
()
047.
048.
With
xlBook.ActiveSheet.Cells(1, 1)
049.
.Value =
"My Customer"
050.
.Font.Bold =
True
051.
.Font.Name =
"Tahoma"
052.
.Font.Size = 16
053.
End
With
054.
055.
With
xlBook.ActiveSheet.Cells(2, 1)
056.
.Value =
"Customer Name"
057.
.Font.Name =
"Tahoma"
058.
.BORDERS.Weight = 1
059.
.Font.Size = 10
060.
.MergeCells =
True
061.
End
With
062.
063.
With
xlBook.ActiveSheet.Cells(2, 2)
064.
.Value =
"Used"
065.
.BORDERS.Weight = 1
066.
.Font.Name =
"Tahoma"
067.
.Font.Size = 10
068.
.MergeCells =
True
069.
End
With
070.
071.
intStartRows = 3
072.
intEndRows = intStartRows + dt.Rows.Count - 1
073.
For
i = 0
To
dt.Rows.Count - 1
074.
xlBook.ActiveSheet.Cells(intStartRows + i, 1).Value = dt.Rows(i)(
"Name"
)
075.
xlBook.ActiveSheet.Cells(intStartRows + i, 2).Value = dt.Rows(i)(
"Used"
)
076.
xlBook.ActiveSheet.Cells(intStartRows + i, 2).NumberFormat =
"$#,##0.00"
077.
Next
078.
079.
080.
xlBook.Charts.Add()
081.
xlBook.ActiveChart.ChartType = 54
082.
xlBook.ActiveChart.SetSourceData(xlBook.Sheets(
"MyReport"
).Range(
"A"
& intStartRows &
":B"
& intEndRows &
""
))
083.
084.
085.
xlBook.ActiveChart.Location(2,
"MyReport"
)
086.
087.
088.
With
xlBook.ActiveChart
089.
.HasTitle =
True
090.
091.
.HasAxis(1) = 1
092.
.ChartTitle.Text =
"Customer Report"
093.
.ChartTitle.Characters.Text =
"Customer Report"
094.
.ChartTitle.Font.Name =
"Tahoma"
095.
.ChartTitle.Font.FontStyle =
"Bold"
096.
.ChartTitle.Font.Size = 20
097.
.ChartTitle.Font.ColorIndex = 3
098.
.Axes(1, 1).HasTitle =
True
099.
.Axes(1, 1).AxisTitle.Characters.Text =
"X"
100.
.Axes(2, 1).HasTitle =
True
101.
.Axes(2, 1).AxisTitle.Characters.Text =
"Y"
102.
.HasDataTable =
False
103.
End
With
104.
105.
106.
xlBook.ActiveSheet.Shapes(
"Chart 1"
).IncrementLeft(20)
107.
xlBook.ActiveSheet.Shapes(
"Chart 1"
).IncrementTop(-97.5)
108.
109.
110.
111.
xlBook.ActiveSheet.Shapes(
"Chart 1"
).ScaleHeight(1.0, 0, 0)
112.
xlBook.ActiveSheet.Shapes(
"Chart 1"
).ScaleWidth(1.0, 0, 0)
113.
114.
115.
Dim
MyFile
As
New
FileInfo(Server.MapPath(FileName))
116.
If
MyFile.Exists
Then
117.
MyFile.Delete()
118.
End
IF
119.
MyFile =
Nothing
120.
121.
122.
123.
xlSheet.SaveAs(Server.MapPath(FileName))
124.
xlApp.Quit()
125.
126.
127.
xlSheet =
Nothing
128.
xlBook =
Nothing
129.
xlApp =
Nothing
130.
131.
132.
133.
Me
.lblText.Text =
"Charts Created <a href="
& FileName &
">Click here</a> to Download."
134.
135.
dtAdapter =
Nothing
136.
objConn.Close()
137.
objConn =
Nothing
138.
End
Sub
139.
</script>
140.
<html>
141.
<head>
142.
<title>ThaiCreate.Com ASP.NET - Excel Application</title>
143.
</head>
144.
<body>
145.
<form id=
"form1"
runat=
"server"
>
146.
<asp:Label id=
"lblText"
runat=
"server"
></asp:Label>
147.
</form>
148.
</body>
149.
</html>