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 %>
posted @ 2019-03-17 17:25  黑旗君  阅读(3122)  评论(0编辑  收藏  举报