ASP+模板產生Word、Excel、html的代碼第1/2頁_應用技巧

來源:互聯網
上載者:User
大多數都是採用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:zhaojiangang@gmail.com
'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)
%>

當前1/2頁  12下一頁閱讀全文

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.