ASP輸出生成Word 、Excel、Txt文件的方法


在ASP中生成Word文件、Excel文件和Txt文件,參考了微軟的官方文檔,自己簡單弄了下,基本可以實現了,不足之處,望指導!下面言歸正傳。

 

1、用ASP生成Word文檔,代碼示例:

 

01

<%

02

Response.ContentType = "application/msword"

03

Response.AddHeader "Content-Disposition", "attachment;filename=NAME.doc"

04

Response.Write("歡迎來到源碼愛好者!<br>" & vbnewline)

05

Response.Write("<h1>用ASP生成Word文檔的示例</h1>")

06

response.write "<table width=""100%"" border=""1"" >"

07

response.write "<tr>"

08

response.write "<th width=""40%""><b>Name</b></th>"

09

response.write "<th width=""30%""><b>Username</b></th>"

10

response.write "<th width=""30%""><b>Password</b></th>"

11

response.write "</tr>"

12

response.write "<tr>"

13

response.write "<td width=""40%"">源碼愛好者</td>"

14

response.write "<td width=""30%"">user</td>"

15

response.write "<td width=""30%"">mypassword</td>"

16

response.write "</tr>"

17

response.write "</table>"

18

%>

用這種方法生成的Word文檔,有時候會出現一個提示:“Microsoft Office Word 需要轉換器以正確顯示該文件。這項功能目前尚未安裝,是否現在安裝?”,這時候重新安裝SKU011.CAB就可以了,原因不明。

 

2、ASP生成Excel文檔:

 

01

<%

02

Response.AddHeader "Content-Disposition", "attachment;filename=members.xls"

03

Response.ContentType = "application/vnd.ms-excel"

04

response.write "<table width=""100%"" border=""1"" >"

05

response.write "<tr>"

06

response.write "<th width=""40%""><b>教程文章</b></th>"

07

response.write "<th width=""30%""><b>Username</b></th>"

08

response.write "<th width=""30%""><b>Password</b></th>"

09

response.write "</tr>"

10

response.write "<tr>"

11

response.write "<td width=""40%"">源碼愛好者</td>"

12

response.write "<td width=""30%"">user</td>"

13

response.write "<td width=""30%"">mypassword</td>"

14

response.write "</tr>"

15

response.write "</table>"

16

%>

3、ASP生成Txt文檔,這個最簡單,用Fso就可以:

 

view sourceprint?

1

<%

2

set objFso = server.createobject("scripting.filesystemobject")

3

set objFile = objFso.CreateTextFile("sample.txt", false)

4

objFile.write "這是一個生成txt文本的演示文檔"

5

objFile.close

6

set objFile = nothing

7

objFso = nothing

8

%>

 

在ASP中生成Word文件、Excel文件和Txt文件,參考了微軟的官方文檔,自己簡單弄了下,基本可以實現了,不足之處,望指導!下面言歸正傳。

1、用ASP生成Word文檔,代碼示例:

01 <%
02 Response.ContentType = "application/msword"
03 Response.AddHeader "Content-Disposition""attachment;filename=NAME.doc"
04 Response.Write("歡迎來到源碼愛好者!<br>" & vbnewline)
05 Response.Write("<h1>用ASP生成Word文檔的示例</h1>")
06 response.write "<table width=""100%"" border=""1"" >"
07 response.write "<tr>"
08 response.write "<th width=""40%""><b>Name</b></th>"
09 response.write "<th width=""30%""><b>Username</b></th>"
10 response.write "<th width=""30%""><b>Password</b></th>"
11 response.write "</tr>"
12 response.write "<tr>"
13 response.write "<td width=""40%"">源碼愛好者</td>"
14 response.write "<td width=""30%"">user</td>"
15 response.write "<td width=""30%"">mypassword</td>"
16 response.write "</tr>"
17 response.write "</table>"
18 %>

用這種方法生成的Word文檔,有時候會出現一個提示:“Microsoft Office Word 需要轉換器以正確顯示該文件。這項功能目前尚未安裝,是否現在安裝?”,這時候重新安裝SKU011.CAB就可以了,原因不明。

2、ASP生成Excel文檔:

01 <%
02 Response.AddHeader "Content-Disposition""attachment;filename=members.xls"
03 Response.ContentType = "application/vnd.ms-excel"
04 response.write "<table width=""100%"" border=""1"" >"
05 response.write "<tr>"
06 response.write "<th width=""40%""><b>教程文章</b></th>"
07 response.write "<th width=""30%""><b>Username</b></th>"
08 response.write "<th width=""30%""><b>Password</b></th>"
09 response.write "</tr>"
10 response.write "<tr>"
11 response.write "<td width=""40%"">源碼愛好者</td>"
12 response.write "<td width=""30%"">user</td>"
13 response.write "<td width=""30%"">mypassword</td>"
14 response.write "</tr>"
15 response.write "</table>"
16 %>

3、ASP生成Txt文檔,這個最簡單,用Fso就可以:

1 <%
2 set objFso = server.createobject("scripting.filesystemobject")
3 set objFile = objFso.CreateTextFile("sample.txt", false)
4 objFile.write "這是一個生成txt文本的演示文檔"
5 objFile.close
6 set objFile = nothing
7 objFso = nothing
8 %>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM