如何创建Word文件?
creatdoc.asp
<!doctype html public "-//w3c/dtd html 3.2 final//en">
<head>
<title>星河影动之创建word文件</title>
<meta http-equiv="refresh" content="30;url='orderform.asp'">
</head>
<%
dotlocation="'servernamedirectory hetemplate.dot'"
introwcount = request.form("rowcount")
' 初始化行计数.
%>
<body language="vbscript" onload="creatdoc document.theform,
<%=dotlocation%>,introwcount>
<form name="theform">
-----------------------------------------------------------------------------------------------------------------------------------
在body标记中对onload调用,调用creatdoc vb脚本子程序,向它传递3个参数:页面中表单的内容(所有的隐含域)、word模板文件的位置、从输入表单中收到的行数.读所有的输入表单域,然后当页面装载后调用creatdoc子程序
<%
itemcount = 0
' 设置域计数器为0.
for each item in request.form
' 总计表格域.
itemcount = itemcount + 1
' 使用for..next 循环.
%>
<input type="hidden" name="<%=item%>" value="<%=request(item)%>">
<% next %>
<input type="hidden" name="numbrows" value="<%=introwcount%>">
<input type="hidden" name="fieldcount" value="<%=itemcount%>">
</form>
</body></html>
-------------------------------------------------------------------------------------------------------------------------------------
开始创建一个word 文件对象. 在internet explorer 4以上版本中,要将浏览器的安全性设置为low或 custom
<%
set objworddoc = createobject("word.document")
objworddoc.application.documents.add thetemplate, false
objworddoc.application.visible=true
%>
-------------------------------------------------------------------------------------------------------------------------------------
调整数组的维数使它与网页表单所包含的行数相同.将y轴设为4个常量,这是输出文件中所需要的栏数,x轴包含从表单中接收的行数
<% redim preserve thearray(4,inttablerows) %>
-------------------------------------------------------------------------------------------------------------------------------------
开始检查所有的表单行.在所有输入的网页表单域中循环,收集每个表单域名及其相应的值.逐个检查并将其放入相应的数列元素内.在此,我们使用了不确定编码的case选择
<%
for intcount = 0 to frmdata.fieldcount.value
strokay = "y"
strsearch = frmdata.elements(intcount).name
' 装入表单域名.
strvalue = frmdata.elements(intcount).value
' 装入表单域值.
strposition = instr(1,strsearch,"_")
intstringlen=strposition-1
if intstrlen > 0 then
strleft = left(strsearch,intstringlen)
strright = right(strsearch,(len(strsearch)-len(strleft)-1))
select case strleft
' select case命令决定表单域属于哪一列.
case "sku" intarrayy=0
case "description" intarrayy=1
case "price" intarrayy=2
case "quantity" intarrayy=3
end select
intarrayx = strright
if strokay <> "n" then
thearray(intarrayy, intarrayx) = strvalue
end if
end if
next
%>
-------------------------------------------------------------------------------------------------------------------------------------
开始创建文件
<%
set rngcurrent = objworddoc.application.activedocument.content
' 对于激活的文件,用变量rngcurrent设置word文件对象range,这是为了防止用户打开另一个文件.
set tabcurrent = objworddoc.application.activedocument.tables.add
rngcurrent,intnumrows,4)
' 通过指定表格的位置( rngcurrent)以及行、列的数目来确定其大小.
%>
------------------------------------------------------------------------------------------------------------------------------------
向往表格里装入数据.
<%
for j = 1 to inttablerows
' 首先指到第一行row(tabrow=1), 逐行循环.在每行结尾处插入回车[chr(10)],以产生行间空行,使行计数器增加.
objworddoc.application.activedocument.tables(1).rows(tabrow).borders.enable=false
objworddoc.application.activedocument.tables(1).rows(tabrow).cells(1).range.insertafter
thearray(1,j)
objworddoc.application.activedocument.tables(1).rows(tabrow).cells(2).range.insertafter
thearray(2,j)
objworddoc.application.activedocument.tables(1).rows(tabrow).cells(3).range.insertafter
formatcurrency(thearray(3,j))
' 用formatcurrency 输出美圆值以保证使用$符号,逗号,小数点的位置.
objworddoc.application.activedocument.tables(1).rows(tabrow).cells(4).range.insertafter
thearray(4,j)
objworddoc.application.activedocument.tables(1).rows(tabrow).cells(4).range.insertafter
chr(10)
objworddoc.applicatoin.activedocument.tables(1).rows(tabrow).cells(3).
range.paragraphformat.alignment=2
' 在paragraphformat. alignment=2处设置栏数来实现美圆数量的正确调整.
tabrow = tabrow + 1
next
%>
------------------------------------------------------------------------------------------------------------------------------------
指定模板位置,结束子程序
<%
objworddoc.application.activedocument.paragraph.add.range.
insertafter("thank you for shopping at acme co., and please come again!")
objworddoc.application.activedocument.paragraph.add.range.insertafter(" ")
objworddoc.application.activedocument.paragraph.add.range.insertafter(" ")
objworddoc.application.activedocument.paragraph.add.range.insertafter("regards,")
objworddoc.application.activedocument.paragraph.add.range.insertafter(" ")
objworddoc.application.activedocument.paragraph.add.range.
insertafter("daryl b. morticum")
objworddoc.application.activedocument.paragraph.add.range.
insertafter("sales associate")
end sub
%>
------------------------------------------------------------------------------------------------------------------------------------