excelchartinasp.asp
来自「Excel大全及示例文件-657M-pdf版.zip」· ASP 代码 · 共 95 行
ASP
95 行
<% @Language=VBSCRIPT %>
<%
'常量声明
Const xlColumnClustered = 51
Const xlRows = 1
Const xlLocationAsObject = 2
Const xlPrimary = 1
Const xlValue = 2
Const xlHtml = 44
'变量声明
dim myExcel '代表Excel应用软件的对象变量
dim fso '代表FileSystemObject的对象变量
dim filename '存储图表文件名称的字符串变量
Dim wkb '代表工作簿对象的对象变量
Dim rng '代表单元格区域对象的对象变量
Set myExcel= Server.Createobject("Excel.Application")
Set fso = Server.Createobject("Scripting.FileSystemObject")
'如果有的话,那么删除之前准备的图表 Line 30
'filename = "C:\ExcelASP\ExcelChartinASP.htm"
filename = "ExcelChartinASP.htm"
If fso.FileExists("C:\ExcelASP\" & filename) Then
fso.DeleteFile "C:\ExcelASP\" & filename, True
End If
'释放FileSystemObject
Set fso = Nothing
'创建一个新工作簿,要确保它含有两个工作表
set wkb = myExcel.Workbooks.Add
'往工作表中添加数据
with wkb.ActiveSheet
.Cells(1,1).Value = "产品类别"
.Cells(2,1).Value = "点心"
.Cells(3,1).Value = "调味品"
.Cells(4,1).Value = "谷类"
.Cells(5,1).Value = "海鲜"
.Cells(6,1).Value = "日用品"
.Cells(7,1).Value = "肉类"
.Cells(8,1).Value = "饮料"
.Cells(1,2).Value = "总销量"
.Cells(2,2).Value = 3580
.Cells(3,2).Value = 1640
.Cells(4,2).Value = 2910
.Cells(5,2).Value = 1280
.Cells(6,2).Value = 5280
.Cells(7,2).Value = 260
.Cells(8,2).Value = 2440
end with
'自适应使用区域的列宽
wkb.ActiveSheet.UsedRange.Columns.Autofit
'设置图表的数据区域
set rng = wkb.Sheets("Sheet1").Range("A1").CurrentRegion
'基于获取的数据创建图表
wkb.Charts.Add
'设置图表格式
wkb.ActiveChart.ChartType = xlColumnClustered
'指定图表数据源
wkb.ActiveChart.SetSourceData rng, xlRows
'放置图表到第二个工作表
wkb.ActiveChart.Location xlLocationAsObject, "Sheet2"
'添加图表和数值轴标题
With wkb.ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "分类别销量"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "销量"
End With
'保存该工作簿为网页(HTML格式)
wkb.SaveAs "C:\ExcelASP\" & filename, xlHtml
'关闭该工作簿
myExcel.ActiveWorkbook.Close
'关闭Excel应用软件并释放对象
myExcel.Quit
Set myExcel = Nothing
' 在网页浏览器里显示创建的网页,注意,在下面的语句中,可能不需要完整路径
Response.Redirect filename
%>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?