ASP操作Excel生成Chart图(引)
1、创建Chart图
objExcelApp.Charts.Add
2、设定Chart图种类
objExcelApp.ActiveChart.ChartType=97
注:二维折线图,4;二维饼图,5;二维柱形图,51
3、设定Chart图标题
objExcelApp.ActiveChart.HasTitle=True
objExcelApp.ActiveChart.ChartTitle.Text="AtestChart"
4、通过表格数据设定图形
objExcelApp.ActiveChart.SetSourceDataobjExcelSheet.Range("A1:k5"),1
5、直接设定图形数据(推荐)
objExcelApp.ActiveChart.SeriesCollection.NewSeries
objExcelApp.ActiveChart.SeriesCollection(1).Name="=""333"""
objExcelApp.ActiveChart.SeriesCollection(1).Values="={1,4,5,6,2}"
6、绑定Chart图
objExcelApp.ActiveChart.Location1
7、显示数据表
objExcelApp.ActiveChart.HasDataTable=True
8、显示图例
objExcelApp.ActiveChart.DataTable.ShowLegendKey=True五、服务器端Excel文件浏览、下载、删除方案
浏览的解决方法很多,“Location.href=”,“Navigate”,“Response.Redirect”都可以实现,建议用客户端的方法,原因是给服务器更多的时间生成Excel文件。
下载的实现要麻烦一些。用网上现成的服务器端下载组件或自己定制开发一个组件是比较好的方案。另外一种方法是在客户端操作Excel组件,由客户端操作服务器端Excel文件另存至客户端。这种方法要求客户端开放不安全ActiveX控件的操作权限,考虑到通知每个客户将服务器设置为可信站点的麻烦程度建议还是用第一个方法比较省事。
删除方案由三部分组成:
A:同一用户生成的Excel文件用同一个文件名,文件名可用用户ID号或SessionID号等可确信不重复字符串组成。这样新文件生成时自动覆盖上一文件。
B:在Global.asa文件中设置Session_onEnd事件激发时,删除这个用户的Excel暂存文件。
C:在Global.asa文件中设置Application_onStart事件激发时,删除暂存目录下的所有文件。
注:建议目录结构\Src代码目录\Templet模板目录\Temp暂存目录六、附录
出错时Excel出现的死进程出现是一件很头疼的事情。在每个文件前加上“OnErrorResumeNext”将有助于改善这种情况,因为它会不管文件是否产生错误都坚持执行到“Application.Quit”,保证每次程序执行完不留下死进程。
补充两点:
1、其他Excel具体操作可以通过录制宏来解决。
2、服务器端打开SQL企业管理器也会产生问题。
<%
OnErrorResumeNextstrAddr=Server.MapPath(".")setobjExcelApp=CreateObject("Excel.Application")
objExcelApp.DisplayAlerts=false
objExcelApp.Application.Visible=false
objExcelApp.WorkBooks.Open(strAddr&"\Templet\Null.xls")
setobjExcelBook=objExcelApp.ActiveWorkBook
setobjExcelSheets=objExcelBook.Worksheets
setobjExcelSheet=objExcelBook.Sheets(1)objExcelSheet.Range("B2:k2").Value=Array("Week1","Week2","Week3","Week4","Week5","Week6","Week7","Week8","Week9","Week10")
objExcelSheet.Range("B3:k3").Value=Array("67","87","5","9","7","45","45","54","54","10")
objExcelSheet.Range("B4:k4").Value=Array("10","10","8","27","33","37","50","54","10","10")
objExcelSheet.Range("B5:k5").Value=Array("23","3","86","64","60","18","5","1","36","80")
objExcelSheet.Cells(3,1).Value="InternetExplorer"
objExcelSheet.Cells(4,1).Value="Netscape"
objExcelSheet.Cells(5,1).Value="Other"objExcelSheet.Range("b2:k5").Select
objExcelApp.Charts.Add
objExcelApp.ActiveChart.ChartType=97
objExcelApp.ActiveChart.BarShape=3
objExcelApp.ActiveChart.HasTitle=True
objExcelApp.ActiveChart.ChartTitle.Text="Visitorslogforeachweekshowninbrowserspercentage"
objExcelApp.ActiveChart.SetSourceDataobjExcelSheet.Range("A1:k5"),1
objExcelApp.ActiveChart.Location1
'objExcelApp.ActiveChart.HasDataTable=True
'objExcelApp.ActiveChart.DataTable.ShowLegendKey=TrueobjExcelBook.SaveAsstrAddr&"\Temp\Excel.xls"objExcelApp.Quit
setobjExcelApp=Nothing
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<TITLE>NewDocument</TITLE>
<METANAME="Generator"CONTENT="MicrosoftFrontPage5.0">
<METANAME="Author"CONTENT="">
<METANAME="Keywords"CONTENT="">
<METANAME="Description"CONTENT="">
</HEAD>
<BODY>
</BODY>
</HTML>
objExcelApp.Charts.Add
2、设定Chart图种类
objExcelApp.ActiveChart.ChartType=97
注:二维折线图,4;二维饼图,5;二维柱形图,51
3、设定Chart图标题
objExcelApp.ActiveChart.HasTitle=True
objExcelApp.ActiveChart.ChartTitle.Text="AtestChart"
4、通过表格数据设定图形
objExcelApp.ActiveChart.SetSourceDataobjExcelSheet.Range("A1:k5"),1
5、直接设定图形数据(推荐)
objExcelApp.ActiveChart.SeriesCollection.NewSeries
objExcelApp.ActiveChart.SeriesCollection(1).Name="=""333"""
objExcelApp.ActiveChart.SeriesCollection(1).Values="={1,4,5,6,2}"
6、绑定Chart图
objExcelApp.ActiveChart.Location1
7、显示数据表
objExcelApp.ActiveChart.HasDataTable=True
8、显示图例
objExcelApp.ActiveChart.DataTable.ShowLegendKey=True五、服务器端Excel文件浏览、下载、删除方案
浏览的解决方法很多,“Location.href=”,“Navigate”,“Response.Redirect”都可以实现,建议用客户端的方法,原因是给服务器更多的时间生成Excel文件。
下载的实现要麻烦一些。用网上现成的服务器端下载组件或自己定制开发一个组件是比较好的方案。另外一种方法是在客户端操作Excel组件,由客户端操作服务器端Excel文件另存至客户端。这种方法要求客户端开放不安全ActiveX控件的操作权限,考虑到通知每个客户将服务器设置为可信站点的麻烦程度建议还是用第一个方法比较省事。
删除方案由三部分组成:
A:同一用户生成的Excel文件用同一个文件名,文件名可用用户ID号或SessionID号等可确信不重复字符串组成。这样新文件生成时自动覆盖上一文件。
B:在Global.asa文件中设置Session_onEnd事件激发时,删除这个用户的Excel暂存文件。
C:在Global.asa文件中设置Application_onStart事件激发时,删除暂存目录下的所有文件。
注:建议目录结构\Src代码目录\Templet模板目录\Temp暂存目录六、附录
出错时Excel出现的死进程出现是一件很头疼的事情。在每个文件前加上“OnErrorResumeNext”将有助于改善这种情况,因为它会不管文件是否产生错误都坚持执行到“Application.Quit”,保证每次程序执行完不留下死进程。
补充两点:
1、其他Excel具体操作可以通过录制宏来解决。
2、服务器端打开SQL企业管理器也会产生问题。
<%
OnErrorResumeNextstrAddr=Server.MapPath(".")setobjExcelApp=CreateObject("Excel.Application")
objExcelApp.DisplayAlerts=false
objExcelApp.Application.Visible=false
objExcelApp.WorkBooks.Open(strAddr&"\Templet\Null.xls")
setobjExcelBook=objExcelApp.ActiveWorkBook
setobjExcelSheets=objExcelBook.Worksheets
setobjExcelSheet=objExcelBook.Sheets(1)objExcelSheet.Range("B2:k2").Value=Array("Week1","Week2","Week3","Week4","Week5","Week6","Week7","Week8","Week9","Week10")
objExcelSheet.Range("B3:k3").Value=Array("67","87","5","9","7","45","45","54","54","10")
objExcelSheet.Range("B4:k4").Value=Array("10","10","8","27","33","37","50","54","10","10")
objExcelSheet.Range("B5:k5").Value=Array("23","3","86","64","60","18","5","1","36","80")
objExcelSheet.Cells(3,1).Value="InternetExplorer"
objExcelSheet.Cells(4,1).Value="Netscape"
objExcelSheet.Cells(5,1).Value="Other"objExcelSheet.Range("b2:k5").Select
objExcelApp.Charts.Add
objExcelApp.ActiveChart.ChartType=97
objExcelApp.ActiveChart.BarShape=3
objExcelApp.ActiveChart.HasTitle=True
objExcelApp.ActiveChart.ChartTitle.Text="Visitorslogforeachweekshowninbrowserspercentage"
objExcelApp.ActiveChart.SetSourceDataobjExcelSheet.Range("A1:k5"),1
objExcelApp.ActiveChart.Location1
'objExcelApp.ActiveChart.HasDataTable=True
'objExcelApp.ActiveChart.DataTable.ShowLegendKey=TrueobjExcelBook.SaveAsstrAddr&"\Temp\Excel.xls"objExcelApp.Quit
setobjExcelApp=Nothing
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<TITLE>NewDocument</TITLE>
<METANAME="Generator"CONTENT="MicrosoftFrontPage5.0">
<METANAME="Author"CONTENT="">
<METANAME="Keywords"CONTENT="">
<METANAME="Description"CONTENT="">
</HEAD>
<BODY>
</BODY>
</HTML>