一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

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

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

2019-09-25 10:19asp代碼網 ASP教程

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

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

用模板生成Excel、Word最大優點:  

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

主要的代碼  
function.asp 

復制代碼代碼如下:


<% 
'歡迎與我交流和學習 
'作者:幸福的子彈 
'BLOG:http://mysheji.com/blog 
'E-mail:[email protected] 
'QQ:37294812 
'----------------------------------------------------------------------------- 
'開啟容錯機制  
on error resume next  
'功能,檢測服務器是否支持指定組件 
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組件,請與管理員聯系!</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功能,請與管理員聯系!</div>" 
    Response.End 
end if 
'----------------------------------------------------------------------------- 
'函數名稱:ReadTextFile 
'作用:利用AdoDb.Stream對象來讀取文本文件 
'參數:FileUrl文件相對路徑,FileCharSet:文件編碼 
Function ReadFromTextFile (FileUrl,FileCharSet)'函數 
    dim str 
    set stm=server.CreateObject("adodb.stream") 
    stm.Type=2 '指定或返回的數據類型, 
    stm.mode=3 '指定打開模式,現在為可以讀寫模式,類似于word的只讀或鎖定功能 
    stm.charset=FileCharSet 
    stm.open 
    stm.loadfromfile server.MapPath(FileUrl) 
    str=stm.readtext 
    ReadFromTextFile=str 
End Function 
'----------------------------------------------------------------------------- 
'函數名稱: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 
'----------------------------------------------------------------------------- 
'功能:自動創建文件夾 
'創建一級或多級目錄,可以創建不存在的根目錄 
'參數:要創建的目錄名稱,可以是多級 
'返回邏輯值,True成功,False失敗 
'創建目錄的根目錄從當前目錄開始 
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 
'點擊下載提示 
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'>網站異常出錯,請與管理員聯系,謝謝!</div>" 
    Response.End 
End If 
%>


生成Word文檔: 

復制代碼代碼如下:


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



生成Excel文檔: 

復制代碼代碼如下:


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


生成.htm靜態頁面 

復制代碼代碼如下:


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

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品黄色 | 国产成人精品s8sp视频 | 久久国产乱子伦精品免费不卡 | 国产自拍影院 | 国产精品综合在线 | 美艳教师刘艳第三部166 | chinese军人@gay | 色久网 | 大陆国产vs国产对白 | 久久精品嫩草影院免费看 | 欧美日韩在线一区二区三区 | 女张腿男人桶羞羞漫画 | 国产精品麻豆99久久 | 成人免费播放 | 久久精品人人做人人爽97 | bl双性小说| adult video在线观看 | 午夜在线播放免费人成无 | 四虎院影永久在线观看 | 女同色图 | 4tube欧美高清| 色综合天天综合中文网 | 亚洲视频一区网站 | 91精品国产色综合久久不卡蜜 | 日韩欧美推理片免费看完整版 | 亚洲精品国产自在现线最新 | a亚洲视频 | 美女国内精品自产拍在线播放 | 啊用力好大粗黑人小说 | 久久伊人久久 | waswaswas免费 | 国产新疆成人a一片在线观看 | 久久视频精品3线视频在线观看 | 97大香伊在人人线色 | 欧美日韩一区二区三区在线观看 | 亚洲波多野结衣日韩在线 | 91狠狠 | 成年性午夜免费视频网站不卡 | caoporn国产| 国产小嫩模好紧 | 黑人video粗暴日本 |