ASP+模板生成Word、Excel、html
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
[p]用模板生成excel、word最大優點: [br][br] word、excel文檔樣式易于控制和調整,以往用excel.application來生成excel、word,需要寫很多代碼來控制排版的樣式,用模版幾乎不受任何限制,只需要打開word或excel,編輯文檔,選擇"文件->另存為web頁",即可方便的做好模板 ,用office生成的模板要比直接在dw中做好模板更加符合office偏好,生成后文件樣式可與原word、excel格式99%一樣,因此建議大家用office(office97~office2003)直接來生成模板框架。 [br][br]主要的代碼 [br]function.asp[/p]
[p][/p]
<%[br]'歡迎與我交流和學習[br]'作者:幸福的子彈[br]'blog:http://mysheji.com/blog[br]'e-mail:zhaojiangang@gmail.com[br]'qq:37294812[br]'-----------------------------------------------------------------------------[br]'開啟容錯機制 [br]on error resume next [br]'功能,檢測服務器是否支持指定組件[br]function object_install(strclassstring)[br]on error resume next[br]object_install=false[br]dim xtestobj[br]set xtestobj=server.createobject(strclassstring)[br]if -2147221005 <> err then object_install=true[br]set xtestobj=nothing[br]end function[br]if object_install("scripting.filesystemobject")=false then[br] response.write "
對不起,您的空間不支持fso組件,請與管理員聯系! "[br] response.end[br]end if[br]if object_install("adodb.stream")=false then[br] response.write "對不起,您的空間不支持adodb.stream功能,請與管理員聯系! "[br] response.end[br]end if[br]'-----------------------------------------------------------------------------[br]'函數名稱:readtextfile[br]'作用:利用adodb.stream對象來讀取文本文件[br]'參數:fileurl文件相對路徑,filecharset:文件編碼[br]function readfromtextfile (fileurl,filecharset)'函數[br] dim str[br] set stm=server.createobject("adodb.stream")[br] stm.type=2 '指定或返回的數據類型,[br] stm.mode=3 '指定打開模式,現在為可以讀寫模式,類似于word的只讀或鎖定功能[br] stm.charset=filecharset[br] stm.open[br] stm.loadfromfile server.mappath(fileurl)[br] str=stm.readtext[br] readfromtextfile=str[br]end function[br]'-----------------------------------------------------------------------------[br]'函數名稱:writetotextfile[br]'作用:利用adodb.stream對象來寫入文本文件[br]sub writetotextfile(fileurl,str,filecharset) '方法[br] set stm=server.createobject("adodb.stream")[br] stm.type=2 [br] stm.mode=3[br] stm.charset=filecharset[br] stm.open[br] stm.writetext str[br] stm.savetofile server.mappath(fileurl),2 [br] stm.flush[br]end sub[br]'-----------------------------------------------------------------------------[br]'功能:自動創建文件夾[br]'創建一級或多級目錄,可以創建不存在的根目錄[br]'參數:要創建的目錄名稱,可以是多級[br]'返回邏輯值,true成功,false失敗[br]'創建目錄的根目錄從當前目錄開始[br]function createmultifolder(byval cfolder)[br]dim objfso,phcreatefolder,createfolderarray,createfolder[br]dim i,ii,createfoldersub,phcreatefoldersub,blinfo[br]blinfo = false[br]createfolder = cfolder[br]on error resume next[br]set objfso = server.createobject("scripting.filesystemobject")[br]if err then[br]err.clear()[br]exit function[br]end if[br]createfolder = replace(createfolder,"","/")[br]if left(createfolder,1)="/" then[br]createfolder = right(createfolder,len(createfolder)-1)[br]end if[br]if right(createfolder,1)="/" then[br]createfolder = left(createfolder,len(createfolder)-1)[br]end if[br]createfolderarray = split(createfolder,"/")[br]for i = 0 to ubound(createfolderarray)[br]createfoldersub = ""[br]for ii = 0 to i[br]createfoldersub = createfoldersub & createfolderarray(ii) & "/"[br]next[br]phcreatefoldersub = server.mappath(createfoldersub)[br]if not objfso.folderexists(phcreatefoldersub) then[br]objfso.createfolder(phcreatefoldersub)[br]end if[br]next[br]if err then[br]err.clear()[br]else[br]blinfo = true[br]end if[br]createmultifolder = blinfo[br]end function[br]'點擊下載提示[br]function downloadfile(strfile)[br] strfilename = server.mappath(strfile)[br] response.buffer = true[br] response.clear[br] set s = server.createobject("adodb.stream")[br] s.open[br] s.type = 1[br] on error resume next[br] set fso = server.createobject("scripting.filesystemobject")[br] if not fso.fileexists(strfilename) then[br] response.write("error:" & strfilename & " does not exist")[br] response.end[br] end if[br] set f = fso.getfile(strfilename)[br] intfilelength = f.size[br] s.loadfromfile(strfilename)[br] if err then[br] response.write(" error:" & err.description & "")[br] response.end[br] end if[br] response.addheader "content-disposition", "attachment; filename=" & f.name[br] response.addheader "content-length", intfilelength[br] response.charset = "utf-8"[br] response.contenttype = "application/octet-stream"[br] response.binarywrite s.read[br] response.flush[br] s.close[br] set s = nothing[br]end function[br]'-----------------------------------------------------------------------------[br]if err then[br] err.clear[br] set conn = nothing[br] response.write " 網站異常出錯,請與管理員聯系,謝謝! "[br] response.end[br]end if[br]%>
[p][br]生成word文檔:[/p]
[p][/p]
[p]<%[br]'創建文件[br]dim templatename,templatechar,filepath,filename,filecharset,templatecontent[br] templatename="template/template_word.htm" '模板名字,支持帶路徑,如"/moban/moban1.htm"或"temp/moban1.htm"[br] templatechar="gb2312" '模板文本的編碼[br] filepath="files/word/" '生成文件保存的路徑,當前目錄請留空,其他目錄,路徑必須以“/”結尾[br] filename="doc1.doc" '即將生成的文件名[br] createmultifolder(filepath) '這一句用來判斷文件夾是否存在,沒有則自動創建,支持n級目錄[br] filecharset="gb2312" '打算生成的文本編碼[br]'讀取指定的模板內容[br]templatecontent=readfromtextfile(templatename,templatechar) [br]'以下就交給你來替換模板內容了[br]templatecontent=replace(templatecontent,"{$websitename}","藍色理想")[br]templatecontent=replace(templatecontent,"{$username}","幸福的子彈")[br]templatecontent=replace(templatecontent,"{$now}",now())[br]'其他內容......[br]'最終調用函數來生成文件 [br]call writetotextfile(filepath&filename,templatecontent,filecharset) [br]'最后關閉adodb.stream對象[br]stm.flush[br]stm.close[br]set stm=nothing[br]downloadfile(filepath&filename)[br]%>[/p]
[p]生成excel文檔:[/p]
<%[br]'創建文件[br]dim templatename,templatechar,filepath,filename,filecharset,templatecontent[br] templatename="template/template_excel.htm" '模板名字,支持帶路徑,如"/moban/moban1.htm"或"temp/moban1.htm"[br] templatechar="gb2312" '模板文本的編碼[br] filepath="files/excel/" '生成文件保存的路徑,當前目錄請留空,其他目錄,路徑必須以“/”結尾[br] filename="book1.xls" '即將生成的文件名[br] createmultifolder(filepath) '這一句用來判斷文件夾是否存在,沒有則自動創建,支持n級目錄[br] filecharset="gb2312" '打算生成的文本編碼[br]'讀取指定的模板內容[br]templatecontent=readfromtextfile(templatename,templatechar) [br]'以下就交給你來替換模板內容了[br]templatecontent=replace(templatecontent,"{$websitename}","藍色理想")[br]templatecontent=replace(templatecontent,"{$username}","幸福的子彈")[br]templatecontent=replace(templatecontent,"{$now}",now())[br]'其他內容......[br]'最終調用函數來生成文件 [br]call writetotextfile(filepath&filename,templatecontent,filecharset) [br]'最后關閉adodb.stream對象[br]stm.flush[br]stm.close[br]set stm=nothing[br]downloadfile(filepath&filename)[br]%>
[p][br]生成.htm靜態頁面[/p]
<%[br]'創建文件[br]dim templatename,templatechar,filepath,filename,filecharset,templatecontent[br]templatename="template/template_html.htm" '模板名字,支持帶路徑,如"/moban/moban1.htm"或"temp/moban1.htm"[br]templatechar="gb2312" '模板文本的編碼[br]filepath="files/html/" '生成文件保存的路徑,當前目錄請留空,其他目錄,路徑必須以“/”結尾[br]filename="untitled-1.htm" '即將生成的文件名[br]createmultifolder(filepath) '這一句用來判斷文件夾是否存在,沒有則自動創建,支持n級目錄[br]filecharset="gb2312" '打算生成的文本編碼[br]'讀取指定的模板內容[br]templatecontent=readfromtextfile(templatename,templatechar) [br]'以下就交給你來替換模板內容了[br]templatecontent=replace(templatecontent,"{$websitename}","藍色理想")[br]templatecontent=replace(templatecontent,"{$username}","幸福的子彈")[br]templatecontent=replace(templatecontent,"{$now}",now())[br]'其他內容......[br]'最終調用函數來生成文件 [br]call writetotextfile(filepath&filename,templatecontent,filecharset) [br]'最后關閉adodb.stream對象[br]stm.flush[br]stm.close[br]set stm=nothing[br]response.write("恭喜您,"&filename&"已經生成,點擊查看")[br]%>
[p][/p]
[p][url=http://sosuo8.com/article/upimages3/aspcreate.rar]打包下載[/url][upload=rar]201073141210-2.rar[/upload][/p]
該文章在 2010/7/3 14:12:15 編輯過 |
關鍵字查詢
相關文章
正在查詢... |