用ASP生成Word、Excel和Txt文档

 网上很多用ASP生成(创建)Word文档的方法都已经随着Word和Excel的版本变更而失效了,在dotnetindex上看到了几篇老外写的文章,简介而且好用,试验了一下还确实不错,放上来保存一下:

1、用ASP生成Word文档

  ASP has the ability to dynamically output any kind of office application format. Before to start coding, The first thing we need to do is set correct file type. Becase the browser needs to know what to do with the file. Second step is to edit file name. You can use HTML and CSS to create styles in your word document.

Here is a comple source code :

<%
Response.ContentType = "application/msword"
Response.AddHeader "Content-Disposition", "attachment;filename=NAME.doc"   
Response.Write("Weste.net : <a href=""http://weste.net"">欢迎您访问西部e网!</a><br>" & vbnewline)
Response.Write("<h1>We can use HTML codes for word documents</h1>")
response.write "<table width=""100%"" border=""1"" >"
response.write "<tr>"
response.write "<th width=""40%""><b>Name</b></th>"
response.write "<th width=""30%""><b>Username</b></th>"
response.write "<th width=""30%""><b>Password</b></th>"
response.write "</tr>"
response.write "<tr>"
response.write "<td width=""40%"">Weste.net</td>"
response.write "<td width=""30%"">admin@weste.net</td>"
response.write "<td width=""30%"">mypassword</td>"
response.write "</tr>"
response.write "</table>"
%>
  说明:用这种方式生成Word文档的时候,出现过一个问题。就是有的时候会显示Word的格式不对。当打开生成的文档时,Word会提示:“Microsoft Office Word 需要转换器以正确显示该文件。这项功能目前尚未安装,是否现在安装?”,需要重新安装SKU011.CAB文件才行。问题我没搞明白,等过几天我真正使用的时候再进行测试。
原文:http://www.dotnetindex.com/articles/3892_Creating_Word_Files_Online.asp

2、用ASP生成Excel文档

ASP has the ability to dynamically output any kind of office application format. Before to start coding, The first thing we need to do is set correct file type. Becase the browser needs to know what to do with the file. Second step is to edit file name. You can use HTML and CSS to create styles in your word document.

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

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

response.write "<table width=""100%"" border=""1"" >"
response.write "<tr>"
response.write "<th width=""40%""><b>Name</b></th>"
response.write "<th width=""30%""><b>Username</b></th>"
response.write "<th width=""30%""><b>Password</b></th>"
response.write "</tr>"
response.write "<tr>"
response.write "<td width=""40%"">Weste.net</td>"
response.write "<td width=""30%"">admin@weste.net</td>"
response.write "<td width=""30%"">mypassword</td>"
response.write "</tr>"
response.write "</table>"
%>

原文:http://www.dotnetindex.com/articles/3893_Creating_Excel_Files_Online.asp

3、用ASP生成Txt文档

In VBScript language there is no any function for Input/Output on hard disk. But you may use FileSystem Object to create and edit files on server. This is an indirect technics to create any file on server.

Here some examples for creating files on server :

<%
set objFso = server.createobject("scripting.filesystemobject")
set objFile = objFso.CreateTextFile("sample.txt", true)
objFile.write "An example creating a file"
objFile.close
set objFile = nothing
objFso = nothing
%>

In this example, server create everytime a new file on same file. If you do not want to create any file with same name, change file mode to false

<%
set objFso = server.createobject("scripting.filesystemobject")
set objFile = objFso.CreateTextFile("sample.txt", false)
objFile.write "An example creating a file"
objFile.close
set objFile = nothing
objFso = nothing
%>

You can not create file, because, you've already create same file.

原文:http://www.dotnetindex.com/articles/5539_Creating_Text_files_on_server.asp

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值