在ASP中怎樣用Excel寫報表
在ASP中怎樣用Excel寫報表,在ASP中怎樣用Excel寫報表
<%
Option Explicit
'页面级对象
'--------------------
Dim m_cn '链接对象
Dim m_rs '记录集对象
Dim m_cspace 'OWC.ChartSpace object
Dim m_fso '文件系统对象
Dim m_objBinaryFile '二进值文件流对象
Dim m_sYear '需要查询的年份
Dim sSQL '执行的SQL 语句
Dim c 'OWC 常量对象
Dim cht '图表临时参考变量
Dim ax '数轴的临时参考变量
Dim fnt 'OWCFont字体的临时参考变量
Dim sFullFileName '输出GIF文件的临时参考路径
'获得查询年份,缺省为2002
m_sYear = Request.QueryString("year")
if len(m_sYear) = 0 then m_sYear = "2002"
'创建一个ADO链接和记录集对象
set m_cn = Server.CreateObject("ADODB.Connection")
set m_rs = Server.CreateObject("ADODB.Recordset")
'链接到Access数据库,使用客户端的游标引擎打开记录集
m_cn.Open "provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath(".") & "data.mdb"
sSQL = "SELECT Month([OrderDate]) AS [Month], Invoices.Salesperson, " & _
"Sum(Invoices.ExtendedPrice) AS Sales FROM Invoices " & _
"WHERE Invoices.OrderDate between #1/1/" & m_sYear & "# and #12/31/" & m_sYear & "# " & _
"GROUP BY Month([OrderDate]), Invoices.Salesperson"
m_rs.CursorLocation = 3 'adUseClient
m_rs.Open sSQL, m_cn, 3 'adOpenStatic
'增加一个带有图例的簇列图表
set m_cspace = server.CreateObject("OWC.Chart")
set m_cspace.DataSource = m_rs
set cht = m_cspace.Charts.Add()
set c = m_cspace.Constants
cht.HasLegend = True
cht.Type =c.chChartTypeLineMarkers
'设置图表的数据源,把销售人添加到序列名称,销售月份添加到分类组,销售金额作为图表中各项取值
cht.SetData c.chDimSeriesNames, 0, "Salesperson"
cht.SetData c.chDimCategories, 0, "Month"
cht.SetData c.chDimValues, 0, "Sales"
cht.SeriesCollection(4).Interior.Color = "blue" '改变第四条线的颜色为蓝色
Cht.SeriesCollection(4).Line.Color = "blue"
'增加一个图表标题,并格式化标题
cht.HasTitle = True
cht.Title.Caption = m_sYear & "的销售金额 "
set fnt = cht.Title.Font
fnt.Name = "宋体"
fnt.Size = 10
fnt.Bold = True
'对分类轴添加标题,并格式化标题
set ax = cht.Axes(c.chAxisPositionBottom)
ax.HasTitle = True
ax.Title.Caption = "月份"
set fnt = ax.Title.Font
fnt.Name = "宋体"
fnt.Size = 8
fnt.Bold = True
'对数值轴添加标题,并格式化标题
set ax = cht.Axes(c.chAxisPositionLeft)
ax.NumberFormat = "Currency"
ax.HasTitle = True
ax