国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - ASP教程 - ASP+模板生成Word、Excel、html的代碼

ASP+模板生成Word、Excel、html的代碼

2019-09-25 10:19asp代碼網(wǎng) ASP教程

由于工作的需要,我需要為客戶做一個在線生成Excel及Word報表程序,參考了網(wǎng)上很多辦法

大多數(shù)都是采用html">Excel.Application(http://www.blueidea.com/tech/program/2006/3547.asp)組件來生成
發(fā)現(xiàn)容易出錯,而且對于大多數(shù)和我一樣的菜鳥來說,比較麻煩,考慮到前些天用ASP+模板+adodb.stream生成靜態(tài)頁面的辦法,經(jīng)過多次嘗試,終于掌握了一種用ASP+模板生成Excel和word的新的辦法,先分享如下:  

用模板生成Excel、Word最大優(yōu)點(diǎn):  

       Word、Excel文檔樣式易于控制和調(diào)整,以往用Excel.Application來生成Excel、Word,需要寫很多代碼來控制排版的樣式,用模版幾乎不受任何限制,只需要打開word或Excel,編輯文檔,選擇"文件->另存為web頁",即可方便的做好模板 ,用office生成的模板要比直接在DW中做好模板更加符合office偏好,生成后文件樣式可與原word、Excel格式99%一樣,因此建議大家用office(office97~office2003)直接來生成模板框架。  

主要的代碼  
function.asp 

復(fù)制代碼代碼如下:


<% 
'歡迎與我交流和學(xué)習(xí) 
'作者:幸福的子彈 
'BLOG:http://mysheji.com/blog 
'E-mail:zhaojiangang@gmail.com 
'QQ:37294812 
'----------------------------------------------------------------------------- 
'開啟容錯機(jī)制  
on error resume next  
'功能,檢測服務(wù)器是否支持指定組件 
Function object_install(strclassstring) 
  on error resume next 
  object_install=false 
  dim xtestobj 
  set xtestobj=server.createobject(strclassstring) 
  if -2147221005 <> Err then object_install=true 
  set xtestobj=nothing 
end function 
if object_install("Scripting.FileSystemobject")=false then 
    Response.Write "<div style='font-size:12px;color:#333;height:20px;line-height:20px;border:1px solid #DDCF8F;padding:6px;background:#FFFFED;font-family:verdana'>對不起,您的空間不支持FSO組件,請與管理員聯(lián)系!</div>" 
    Response.End 
end if 
if object_install("adodb.stream")=false then 
    Response.Write "<div style='font-size:12px;color:#333;height:20px;line-height:20px;border:1px solid #DDCF8F;padding:6px;background:#FFFFED;font-family:verdana'>對不起,您的空間不支持adodb.stream功能,請與管理員聯(lián)系!</div>" 
    Response.End 
end if 
'----------------------------------------------------------------------------- 
'函數(shù)名稱:ReadTextFile 
'作用:利用AdoDb.Stream對象來讀取文本文件 
'參數(shù):FileUrl文件相對路徑,F(xiàn)ileCharSet:文件編碼 
Function ReadFromTextFile (FileUrl,FileCharSet)'函數(shù) 
    dim str 
    set stm=server.CreateObject("adodb.stream") 
    stm.Type=2 '指定或返回的數(shù)據(jù)類型, 
    stm.mode=3 '指定打開模式,現(xiàn)在為可以讀寫模式,類似于word的只讀或鎖定功能 
    stm.charset=FileCharSet 
    stm.open 
    stm.loadfromfile server.MapPath(FileUrl) 
    str=stm.readtext 
    ReadFromTextFile=str 
End Function 
'----------------------------------------------------------------------------- 
'函數(shù)名稱:WriteToTextFile 
'作用:利用AdoDb.Stream對象來寫入文本文件 
sub WriteToTextFile(FileUrl,Str,FileCharSet) '方法 
    set stm=server.CreateObject("adodb.stream") 
    stm.Type=2  
    stm.mode=3 
    stm.charset=FileCharSet 
    stm.open 
    stm.WriteText str 
    stm.SaveToFile server.MapPath(FileUrl),2  
    stm.flush 
End sub 
'----------------------------------------------------------------------------- 
'功能:自動創(chuàng)建文件夾 
'創(chuàng)建一級或多級目錄,可以創(chuàng)建不存在的根目錄 
'參數(shù):要創(chuàng)建的目錄名稱,可以是多級 
'返回邏輯值,True成功,F(xiàn)alse失敗 
'創(chuàng)建目錄的根目錄從當(dāng)前目錄開始 
Function CreateMultiFolder(ByVal CFolder) 
Dim objFSO,PhCreateFolder,CreateFolderArray,CreateFolder 
Dim i,ii,CreateFolderSub,PhCreateFolderSub,BlInfo 
BlInfo = False 
CreateFolder = CFolder 
On Error Resume Next 
Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 
If Err Then 
Err.Clear() 
Exit Function 
End If 
CreateFolder = Replace(CreateFolder,"","/") 
If Left(CreateFolder,1)="/" Then 
CreateFolder = Right(CreateFolder,Len(CreateFolder)-1) 
End If 
If Right(CreateFolder,1)="/" Then 
CreateFolder = Left(CreateFolder,Len(CreateFolder)-1) 
End If 
CreateFolderArray = Split(CreateFolder,"/") 
For i = 0 to UBound(CreateFolderArray) 
CreateFolderSub = "" 
For ii = 0 to i 
CreateFolderSub = CreateFolderSub & CreateFolderArray(ii) & "/" 
Next 
PhCreateFolderSub = Server.MapPath(CreateFolderSub) 
If Not objFSO.FolderExists(PhCreateFolderSub) Then 
objFSO.CreateFolder(PhCreateFolderSub) 
End If 
Next 
If Err Then 
Err.Clear() 
Else 
BlInfo = True 
End If 
CreateMultiFolder = BlInfo 
End Function 
'點(diǎn)擊下載提示 
function downloadFile(strFile) 
     strFilename = server.MapPath(strFile) 
     Response.Buffer = True 
     Response.Clear 
     Set s = Server.CreateObject("ADODB.Stream") 
     s.Open 
     s.Type = 1 
     on error resume next 
     Set fso = Server.CreateObject("Scripting.FileSystemObject") 
     if not fso.FileExists(strFilename) then 
         Response.Write("<h1>Error:</h1>" & strFilename & " does not exist<p>") 
         Response.End 
     end if 
     Set f = fso.GetFile(strFilename) 
     intFilelength = f.size 
     s.LoadFromFile(strFilename) 
     if err then 
         Response.Write("<h1>Error: </h1>" & err.Description & "<p>") 
         Response.End 
     end if 
     Response.AddHeader "Content-Disposition", "attachment; filename=" & f.name 
     Response.AddHeader "Content-Length", intFilelength 
     Response.CharSet = "UTF-8" 
     Response.ContentType = "application/octet-stream" 
     Response.BinaryWrite s.Read 
     Response.Flush 
     s.Close 
     Set s = Nothing 
End Function 
'----------------------------------------------------------------------------- 
If Err Then 
    err.Clear 
    Set conn = Nothing 
    Response.Write "<div style='font-size:12px;color:#333;height:20px;line-height:20px;border:1px solid #DDCF8F;padding:6px;background:#FFFFED;font-family:verdana'>網(wǎng)站異常出錯,請與管理員聯(lián)系,謝謝!</div>" 
    Response.End 
End If 
%>


生成Word文檔: 

復(fù)制代碼代碼如下:


<% 
'創(chuàng)建文件 
dim templateName,templatechar,filepath,filename,fileCharset,templateContent 
   templateName="template/template_word.htm"        '模板名字,支持帶路徑,如"/moban/moban1.htm"或"temp/moban1.htm" 
   templatechar="gb2312"                      '模板文本的編碼 
   filepath="files/word/"                     '生成文件保存的路徑,當(dāng)前目錄請留空,其他目錄,路徑必須以“/”結(jié)尾 
   filename="Doc1.doc"                           '即將生成的文件名 
   CreateMultiFolder(filepath)                '這一句用來判斷文件夾是否存在,沒有則自動創(chuàng)建,支持n級目錄 
   fileCharset="gb2312"                       '打算生成的文本編碼 
'讀取指定的模板內(nèi)容 
templateContent=ReadFromTextFile(templateName,templatechar)    
'以下就交給你來替換模板內(nèi)容了 
templateContent=replace(templateContent,"{$websiteName}","藍(lán)色理想") 
templateContent=replace(templateContent,"{$userName}","幸福的子彈") 
templateContent=replace(templateContent,"{$now}",Now()) 
'其他內(nèi)容...... 
'最終調(diào)用函數(shù)來生成文件          
Call WriteToTextFile(filepath&filename,templateContent,fileCharset)    
'最后關(guān)閉adodb.stream對象 
stm.flush 
stm.Close 
set stm=nothing 
downloadFile(filepath&filename) 
%>



生成Excel文檔: 

復(fù)制代碼代碼如下:


<%  
'創(chuàng)建文件  
dim templateName,templatechar,filepath,filename,fileCharset,templateContent  
   templateName="template/template_excel.htm"        '模板名字,支持帶路徑,如"/moban/moban1.htm"或"temp/moban1.htm"  
   templatechar="gb2312"                      '模板文本的編碼  
   filepath="files/excel/"                     '生成文件保存的路徑,當(dāng)前目錄請留空,其他目錄,路徑必須以“/”結(jié)尾  
   filename="Book1.xls"                           '即將生成的文件名  
   CreateMultiFolder(filepath)                '這一句用來判斷文件夾是否存在,沒有則自動創(chuàng)建,支持n級目錄  
   fileCharset="gb2312"                       '打算生成的文本編碼  
'讀取指定的模板內(nèi)容  
templateContent=ReadFromTextFile(templateName,templatechar)     
'以下就交給你來替換模板內(nèi)容了  
templateContent=replace(templateContent,"{$websiteName}","藍(lán)色理想")  
templateContent=replace(templateContent,"{$userName}","幸福的子彈")  
templateContent=replace(templateContent,"{$now}",Now())  
'其他內(nèi)容......  
'最終調(diào)用函數(shù)來生成文件           
Call WriteToTextFile(filepath&filename,templateContent,fileCharset)     
'最后關(guān)閉adodb.stream對象  
stm.flush  
stm.Close  
set stm=nothing  
downloadFile(filepath&filename)  
%> 


生成.htm靜態(tài)頁面 

復(fù)制代碼代碼如下:


<%  
'創(chuàng)建文件  
dim templateName,templatechar,filepath,filename,fileCharset,templateContent  
   templateName="template/template_html.htm"        '模板名字,支持帶路徑,如"/moban/moban1.htm"或"temp/moban1.htm"  
   templatechar="gb2312"                      '模板文本的編碼  
   filepath="files/html/"                     '生成文件保存的路徑,當(dāng)前目錄請留空,其他目錄,路徑必須以“/”結(jié)尾  
   filename="Untitled-1.htm"                           '即將生成的文件名  
   CreateMultiFolder(filepath)                '這一句用來判斷文件夾是否存在,沒有則自動創(chuàng)建,支持n級目錄  
   fileCharset="gb2312"                       '打算生成的文本編碼  
'讀取指定的模板內(nèi)容  
templateContent=ReadFromTextFile(templateName,templatechar)     
'以下就交給你來替換模板內(nèi)容了  
templateContent=replace(templateContent,"{$websiteName}","藍(lán)色理想")  
templateContent=replace(templateContent,"{$userName}","幸福的子彈")  
templateContent=replace(templateContent,"{$now}",now())  
'其他內(nèi)容......  
'最終調(diào)用函數(shù)來生成文件           
Call WriteToTextFile(filepath&filename,templateContent,fileCharset)     
'最后關(guān)閉adodb.stream對象  
stm.flush  
stm.Close  
set stm=nothing  
response.Write("恭喜您,"&filename&"已經(jīng)生成,<a href="""&filepath&filename&""" target=""_blank"">點(diǎn)擊查看</a>")  
%>

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产情侣av自拍 | 国产成人亚洲精品 | 日韩在线视频在线观看 | 99亚洲伊人久久精品影院 | 国产精品久久久久久久久 | 在线一区视频 | 在线国产一区二区 | 欧美成年黄网站色视频 | 中文字幕人成乱码在线观看 | 亚洲午夜在线 | 日韩电影一区二区在线观看 | 噜噜噜在线观看免费视频日本 | 操操操小说 | 精品国产一区二区三区高潮视 | 精品在线一区 | 欧美黑人一级爽快片淫片高清 | 日韩精品极品视频在线观看免费 | 成人伊人| 国产成人精品免费视频大全最热 | 蜜桃色网 | 精品成人免费一区二区在线播放 | 久久精品免费 | 国产精品国产精品国产专区不卡 | 欧美精品国产精品 | 久久综合九色综合欧美狠狠 | 伊人中文 | 成人午夜网站 | 久久天天躁狠狠躁夜夜躁2014 | 欧美精品一区二区三区在线 | 日日干夜夜操 | 久久精视频 | 午夜欧美一区二区三区在线播放 | 日韩精品极品视频在线观看免费 | 狠狠综合久久av一区二区老牛 | 欧美一区二区三区在线观看视频 | 欧美日韩成人一区 | 国产美女www | 九九九久久国产免费 | 欧美成人免费在线视频 | 久久精品99久久 | 男人的天堂久久精品 |