1. 程式人生 > >ASP+模板生成Word、Excel、html

ASP+模板生成Word、Excel、html

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


主要的程式碼
function.asp

<%
'開啟容錯機制
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
%>

例項生成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)
%>

生成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)
%>

生成.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>")
%>