四、ASP操作Excel生成Chart圖
1、創(chuàng)建Chart圖 objExcelApp.Charts.Add 2、設(shè)定Chart圖種類 objExcelApp.ActiveChart.ChartType=97 注:二維折線圖,4;二維餅圖,5;二維柱形圖,51 3、設(shè)定Chart圖標(biāo)題 objExcelApp.ActiveChart.HasTitle=True objExcelApp.ActiveChart.ChartTitle.Text="AtestChart" 4、通過表格數(shù)據(jù)設(shè)定圖形 objExcelApp.ActiveChart.SetSourceDataobjExcelSheet.Range("A1:k5"),1 5、直接設(shè)定圖形數(shù)據(jù)(推薦) 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、顯示數(shù)據(jù)表 objExcelApp.ActiveChart.HasDataTable=True 8、顯示圖例 objExcelApp.ActiveChart.DataTable.ShowLegendKey=True
五、服務(wù)器端Excel文件瀏覽、下載、刪除方案
瀏覽的解決方法很多,“Location.href=”,“Navigate”,“Response.Redirect”都可以實(shí)現(xiàn),建議用客戶端的方法,原因是給服務(wù)器更多的時(shí)間生成Excel文件。 下載的實(shí)現(xiàn)要麻煩一些。用網(wǎng)上現(xiàn)成的服務(wù)器端下載組件或自己定制開發(fā)一個組件是比較好的方案。另外一種方法是在客戶端操作Excel組件,由客戶端操作服務(wù)器端Excel文件另存至客戶端。這種方法要求客戶端開放不安全ActiveX控件的操作權(quán)限,考慮到通知每個客戶將服務(wù)器設(shè)置為可信站點(diǎn)的麻煩程度建議還是用第一個方法比較省事。
刪除方案由三部分組成:
A:同一用戶生成的Excel文件用同一個文件名,文件名可用用戶ID號或SessionID號等可確信不重復(fù)字符串組成。這樣新文件生成時(shí)自動覆蓋上一文件。 B:在Global.asa文件中設(shè)置Session_onEnd事件激發(fā)時(shí),刪除這個用戶的Excel暫存文件。 C:在Global.asa文件中設(shè)置Application_onStart事件激發(fā)時(shí),刪除暫存目錄下的所有文件。 注:建議目錄結(jié)構(gòu)\Src代碼目錄\Templet模板目錄\Temp暫存目錄
六、附錄
出錯時(shí)Excel出現(xiàn)的死進(jìn)程出現(xiàn)是一件很頭疼的事情。在每個文件前加上“OnErrorResumeNext”將有助于改善這種情況,因?yàn)樗鼤还芪募欠癞a(chǎn)生錯誤都堅(jiān)持執(zhí)行到“Application.Quit”,保證每次程序執(zhí)行完不留下死進(jìn)程。
補(bǔ)充兩點(diǎn):
1、其他Excel具體操作可以通過錄制宏來解決。 2、服務(wù)器端打開SQL企業(yè)管理器也會產(chǎn)生問題。
<% 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>
出處:AppleBBS的Blog
責(zé)任編輯:moby
上一頁 ASP操作Excel技術(shù)總結(jié) [1] 下一頁
◎進(jìn)入論壇網(wǎng)絡(luò)編程版塊參加討論
|