当前位置:网络资源中心文章中心网络编程ASP教程 → 文章内容

如何用ASP建立图表 (1)

减小字体 增大字体 作者:阿辉  来源:网咯收集  发布时间:2008-5-16 9:47:08
现在我们针对EXCEL举另外一个例子,大家都询问如何用ASP建立图表的问题,以下就是解决方案

First we set the type of script
首先设定脚本类型
%26lt;%@ LANGUAGE=%26quot;VBSCRIPT%26quot; %%26gt;

Make the object, and set the object to an Excelsheet
建立Excelsheet对象

Dim MyExcelChart
Set MyExcelChart = CreateObject(%26quot;Excel.Sheet%26quot;)

' show or dont show excel to user, TRUE or FALSE
是否让用户看到EXCEL表格,真或假
MyExcelChart.Application.Visible = True

' populate the cells
添EXCEL表格
MyExcelChart.ActiveSheet.Range(%26quot;B2:k2%26quot;).Value = Array(%26quot;Week1%26quot;, %26quot;Week2%26quot;, %26quot;Week3%26quot;, %26quot;Week4%26quot;, %26quot;Week5%26quot;, %26quot;Week6%26quot;, %26quot;Week7%26quot;, %26quot;Week8%26quot;, %26quot;Week9%26quot;, %26quot;Week10%26quot;)
MyExcelChart.ActiveSheet.Range(%26quot;B3:k3%26quot;).Value = Array(%26quot;67%26quot;, %26quot;87%26quot;, %26quot;5%26quot;, %26quot;9%26quot;, %26quot;7%26quot;, %26quot;45%26quot;, %26quot;45%26quot;, %26quot;54%26quot;, %26quot;54%26quot;, %26quot;10%26quot;)
MyExcelChart.ActiveSheet.Range(%26quot;B4:k4%26quot;).Value = Array(%26quot;10%26quot;, %26quot;10%26quot;, %26quot;8%26quot;, %26quot;27%26quot;, %26quot;33%26quot;, %26quot;37%26quot;, %26quot;50%26quot;, %26quot;54%26quot;, %26quot;10%26quot;, %26quot;10%26quot;)
MyExcelChart.ActiveSheet.Range(%26quot;B5:k5%26quot;).Value = Array(%26quot;23%26quot;, %26quot;3%26quot;, %26quot;86%26quot;, %26quot;64%26quot;, %26quot;60%26quot;, %26quot;18%26quot;, %26quot;5%26quot;, %26quot;1%26quot;, %26quot;36%26quot;, %26quot;80%26quot;)
MyExcelChart.ActiveSheet.Cells(3,1).Value=%26quot;Internet Explorer%26quot;
MyExcelChart.ActiveSheet.Cells(4,1).Value=%26quot;Netscape%26quot;
MyExcelChart.ActiveSheet.Cells(5,1).Value=%26quot;Other%26quot;

' select the contents that need to be in the chart
在EXCEL表中选择要在图表(CHART)中显示的数据
MyExcelChart.ActiveSheet.Range(%26quot;b2:k5%26quot;).Select

' Add the chart
加载图表(CHART)
MyExcelChart.Charts.Add
' Format the chart, set type of chart, shape of the bars, show title, get the data for the chart, show datatable, show legend
初始化图表(CHART),设定图表类型,棒图的外形,要显示的标题,取得要作图的数据,显示数据表,显示图表
MyExcelChart.activechart.ChartType = 97
MyExcelChart.activechart.BarShape =3
MyExcelChart.activechart.HasTitle = True
MyExcelChart.activechart.ChartTitle.Text = %26quot;Visitors log for each week shown in browsers percentage%26quot;
MyExcelChart.activechart.SetSourceData MyExcelChart.Sheets(%26quot;Sheet1%26quot;).Range(%26quot;A1:k5%26quot;),1
MyExcelChart.activechart.Location 1
MyExcelChart.activechart.HasDataTable = True
MyExcelChart.activechart.DataTable.ShowLegendKey = True

' Save the the excelsheet to excelface
存入EXCEL表
MyExcelChart.SaveAs %26quot;c:\chart.xls%26quot;
%%26gt;

Now lets complete the HTML tags.
下面是HTML代码

%26lt;HTML%26gt;
%26lt;HEAD%26gt;
%26lt;TITLE%26gt;MyExcelChart%26lt;/TITLE%26gt;
%26lt;/HEAD%26gt;
%26lt;BODY%26gt;
%26lt;/BODY%26gt;
%26lt;/HTML%26gt;

This completes yer ASP page, look below for the complete code of myexcelchart.asp
下面是源程序myexcelchart.asp的完整代码

%26lt;%@ LANGUAGE=%26quot;VBSCRIPT%26quot; %%26gt;
%26lt;%
Set MyExcelChart = CreateObject(%26quot;Excel.Sheet%26quot;)

MyExcelChart.Application.Visible = True

MyExcelChart.ActiveSheet.Range(%26quot;B2:k2%26quot;).Value = Array(%26quot;Week1%26quot;, %26quot;Week2%26quot;, %26quot;Week3%26quot;, %26quot;Week4%26quot;, %26quot;Week5%26quot;, %26quot;Week6%26quot;, %26quot;Week7%26quot;, %26quot;Week8%26quot;, %26quot;Week9%26quot;, %26quot;Week10%26quot;)
MyExcelChart.ActiveSheet.Range(%26quot;B3:k3%26quot;).Value = Array(%26quot;67%26quot;, %26quot;87%26quot;, %26quot;5%26quot;, %26quot;9%26quot;, %26quot;7%26quot;, %26quot;45%26quot;, %26quot;45%26quot;, %26quot;54%26quot;, %26quot;54%26quot;, %26quot;10%26quot;)
MyExcelChart.ActiveSheet.Range(%26quot;B4:k4%26quot;).Value = Array(%26quot;10%26quot;, %26quot;10%26quot;, %26quot;8%26quot;, %26quot;27%26quot;, %26quot;33%26quot;, %26quot;37%26quot;, %26quot;50%26quot;, %26quot;54%26quot;, %26quot;10%26quot;, %26quot;10%26quot;)
MyExcelChart.ActiveSheet.Range(%26quot;B5:k5%26quot;).Value = Array(%26quot;23%26quot;, %26quot;3%26quot;, %26quot;86%26quot;, %26quot;64%26quot;, %26quot;60%26quot;, %26quot;18%26quot;, %26quot;5%26quot;, %26quot;1%26quot;, %26quot;36%26quot;, %26quot;80%26quot;)
MyExcelChart.ActiveSheet.Cells(3,1).Value=%26quot;Internet Explorer%26quot;
MyExcelChart.ActiveSheet.Cells(4,1).Value=%26quot;Netscape%26quot;
MyExcelChart.ActiveSheet.Cells(5,1).Value=%26quot;Other%26quot;

MyExcelChart.ActiveSheet.Range(%26quot;b2:k5%26quot;).Select

MyExcelChart.Charts.Add
MyExcelChart.activechart.ChartType = 97
MyExcelChart.activechart.BarShape =3
MyExcelChart.activechart.HasTitle = True
MyExcelChart.activechart.ChartTitle.Text = %26quot;Visitors log for each week shown in browsers percentage%26quot;
MyExcelChart.activechart.SetSourceData MyExcelChart.Sheets(%26quot;Sheet1%26quot;).Range(%26quot;A1:k5%26quot;),1
MyExcelChart.activechart.Location 1
MyExcelChart.activechart.HasDataTable = True
MyExcelChart.activechart.DataTable.ShowLegendKey = True

MyExcelChart.SaveAs %26quot;c:\chart.xls%26quot;

%%26gt;
%26lt;HTML%26gt;
%26lt;HEAD%26gt;
%26lt;TITLE%26gt;MyExcelChart%26lt;/TITLE%26gt;
%26lt;/HEAD%26gt;
%26lt;BODY%26gt;
%26lt;/BODY%26gt;
%26lt;/HTML%26gt;