濮阳杆衣贸易有限公司

主頁 > 知識庫 > ASP+模板生成Word、Excel、html的代碼第1/2頁

ASP+模板生成Word、Excel、html的代碼第1/2頁

熱門標簽:外呼系統(tǒng)怎么話費 電銷機器人采購 友邦互聯電銷機器人違法嗎 高質量的電銷外呼系統(tǒng) 宿州防封外呼系統(tǒng)平臺 硅基電話機器人加盟 無營業(yè)執(zhí)照地圖標注教學 地圖標注還可以做嗎 滴滴地圖標注上車點
大多數都是采用Excel.Application(http://www.blueidea.com/tech/program/2006/3547.asp)組件來生成
發(fā)現容易出錯,而且對于大多數和我一樣的菜鳥來說,比較麻煩,考慮到前些天用ASP+模板+adodb.stream生成靜態(tài)頁面的辦法,經過多次嘗試,終于掌握了一種用ASP+模板生成Excel和word的新的辦法,先分享如下: 

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

       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
'-----------------------------------------------------------------------------
'功能:自動創(chuàng)建文件夾
'創(chuàng)建一級或多級目錄,可以創(chuàng)建不存在的根目錄
'參數:要創(chuàng)建的目錄名稱,可以是多級
'返回邏輯值,True成功,False失敗
'創(chuà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
'點擊下載提示
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 existp>")
         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文檔:
復制代碼 代碼如下:

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

12下一頁閱讀全文
您可能感興趣的文章:
  • php Smarty模板生成html文檔的方法
  • JavaScript和ActionScript的交互實現代碼
  • PHP JSON格式數據交互實例代碼詳解
  • thinkPHP的Html模板標簽使用方法
  • 解析Android中webview和js之間的交互
  • PHP與javascript實現變量交互的示例代碼
  • 多種方式實現JS調用后臺方法進行數據交互
  • extjs中form與grid交互數據(record)的方法
  • C#.NET采用HTML模板發(fā)送電子郵件完整實例
  • 基于HTML模板和JSON數據的JavaScript交互(移動端)

標簽:新余 雅安 廣元 宣城 七臺河 儋州 錫林郭勒盟 江門

巨人網絡通訊聲明:本文標題《ASP+模板生成Word、Excel、html的代碼第1/2頁》,本文關鍵詞  ASP+,模板,生成,Word,Excel,;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《ASP+模板生成Word、Excel、html的代碼第1/2頁》相關的同類信息!
  • 本頁收集關于ASP+模板生成Word、Excel、html的代碼第1/2頁的相關信息資訊供網民參考!
  • 推薦文章
    南宁市| 九龙坡区| 南靖县| 申扎县| 博乐市| 灯塔市| 靖州| 扶沟县| 昌邑市| 宝清县| 渭南市| 玉山县| 巴彦县| 富蕴县| 曲沃县| 宜章县| 定日县| 同仁县| 古浪县| 科技| 岫岩| 道孚县| 枣阳市| 淳化县| 台中县| 合水县| 绿春县| 甘谷县| 阿坝县| 疏附县| 平江县| 海晏县| 日照市| 达尔| 乳山市| 陕西省| 陈巴尔虎旗| 湄潭县| 皋兰县| 巴青县| 上林县|