您的位置:首页-> 资讯中心-> 网络编程-> ASP技术-> 在ASP中怎樣用Excel寫報表
ASP技术
ASP技术
.Net专区
数据库类
JSP
本类阅读TOP10
·ASP对excel的操作。
·问题
·如何利用回车来进行焦点转移呢?
·ASP操作Excel技术总结
·在ASP中怎樣用Excel寫報表
·如何用asp调用启动服务器上的Exe程序。
·利用XMLHTTP的国际域名查询系统源代码(一个页面,无需另外组件,原帖另附QQ在线状态检测)
·用VB编写ActiveX DLL实现ASP编程
·在第一个输入框输入完以后,回车如何自动转移到第二个输入框在焦点呢?
·ASP如何获取真实IP地址
广告连接
精选专题
在ASP中怎樣用Excel寫報表
作者:未知 来源:未知 加入时间:2004-8-28

在ASP中﹐怎樣將Sql Server或者Oracle數據庫的資料轉成Excel圖表(直條圖和折線圖)﹐并在網頁顯示出來?
最好有實例。
---------------------------------------------------------------

http://www.csdn.net/Subject/15/index.shtm
---------------------------------------------------------------

OWC,具体使用可以看OFFICE安装目录下2052里的MSOWC.CHM

柱图
<%@ Language=VBScript %>
<%
Option Explicit
'Response.Expires = -1
'Response.ContentType = "image/gif"

'页面级对象
'--------------------
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 cht = m_cspace.Charts.Add()
set c = m_cspace.Constants

set m_cspace.DataSource = m_rs

cht.Type = 1
cht.HasLegend = True

'设置图表的数据源,把销售人添加到序列名称,销售月份添加到分类组,销售金额作为图表中各项取值


cht.SetData c.chDimSeriesNames, 0, "Salesperson"
cht.SetData c.chDimCategories, 0, "Month"
cht.SetData c.chDimValues, 0, "Sales"
c.HasPercentage = True
c.HasValue = False

'增加一个图表标题,并格式化标题
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.Title.Caption = "元"
set fnt = ax.Title.Font
fnt.Name = "宋体"
fnt.Size = 8
fnt.Bold = True
fnt.color="red"

'用一个临时变量名保存当前文件,这个文件名是唯一的
set m_fso = CreateObject("Scripting.FileSystemObject")
sFullFileName = Server.MapPath(".") & "\" & m_fso.GetTempName()
m_cspace.ExportPicture sFullFileName, "GIF", 800, 400

'使用 On Error Resume Next语句是为了确保我们删除了临时文件,即使一些函数调用失败
on error resume next

'GIF文件已经输出,我们可以通过COM组件把它的内容发送到客户端
set m_objBinaryFile = server.CreateObject("BinFileWrite.GetFileStream")

m_objBinaryFile.SendBinFile CStr(sFullFileName),"image/GIF",TRUE,FALSE,TRUE,TRUE,TRUE
'GIF文件已经不需要了可以删除了
m_objBinaryFile.DeleteFile CStr(sFullFileName)

%>

饼图
<%@ Language=VBScript %>






利用OWC动态生成图表



<%
set m_cspace = server.CreateObject("OWC.Chart")
set DSC = server.CreateObject("OWC.DataSourceControl")
str="provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath(".") & "\data.mdb"
'Response.Write (str)
DSC.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath(".") & "\data.mdb"

DSC.RecordsetDefs.AddNew "SELECT Month([OrderDate]) AS [Month], Invoices.Salesperson, " & _
  "Sum(Invoices.ExtendedPrice) AS Sales FROM Invoices " & _
  "WHERE Invoices.OrderDate between #1/1/2002# and #12/31/2002# " & _
  "GROUP BY Month([OrderDate]), Invoices.Salesperson",DSC.Constants.dscCommandText,"ChartData"

'BindChartToDSC m_m_cspace, DSC, "ChartData", "month", "Sales"
set c = m_cspace.Constants

m_cspace.Clear

' 绑定数据源
set m_cspace.DataSource = dsc

m_cspace.DataMember = ChartData

set cht = m_cspace.Charts.Add()
cht.HasLegend = True
cht.Type = 19

'增加一个图表标题,并格式化标题
cht.HasTitle = True
cht.Title.Caption = m_sYear & "的销售金额 "
set fnt = cht.Title.Font
fnt.Name = "宋体"
fnt.Size = 10
fnt.Bold = True

set ser = cht.SeriesCollection.Add()
ser.SetData c.chDimCategories, 0, "Salesperson"
ser.SetData c.chDimValues, 0, "Sales"

set dls = ser.DataLabelsCollection.Add()
dls.HasPercentage = True
dls.HasValue = False

'm_sFilePath = ExportChartToGIF(m_m_cspace)
set m_fso = CreateObject("Scripting.FileSystemObject")
sFullFileName = Server.MapPath(".") & "\" & m_fso.GetTempName()
m_cspace.ExportPicture sFullFileName, "GIF", 800, 400

'on error resume next
set m_objBinaryFile = server.CreateObject("BinFileWrite.GetFileStream")
Response.BinaryWrite m_objBinaryFile.GetFileBytes(CStr(sFullFileName))
m_objBinaryFile.SendBinFile CStr(sFullFileName),"image/GIF",TRUE,FALSE,TRUE,TRUE,TRUE
set fso=nothing
set m_m_cspace=nothing
set m_cht=nothing
%>








折线图
<%@ Language=VBScript %>
<%
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


相关软件
  • Excel匹配工具 2.1 让你的字段按照须要提取部分进行匹配
  • Excel数据录入、修改助手 1.1 简体中文版 适用于大量Excel数据录入和修改
  • Excel学友 V5.8 手把手动画语音教你学习Excel
  • EXCEL教学成绩统计系统 V7.5 全自动的教学成绩统计软件
  • Excel登分王 V3.09 通用版 随手输入的考号、成绩各就各位。
  • MS Excel File Viewer v1.69 - xls文件阅读器
  • 勤哲Excel服务器 2006 V6.6 标准版
  • 勤哲Excel服务器 2006 企业版客户端6.6
  • 勤哲Excel服务器 2006 V6.6 完整企业版
  • 勤哲Excel服务器 2006 V6.6 企业版
  • 相关文章
  • 在Excel单元格中设置斜线
  • ASP对excel的操作。
  • ASP操作Excel技术总结