读书人

Asp获取数据插入Word模板时,Word内容循

发布时间: 2013-04-12 18:33:11 作者: rapoo

Asp获取数据插入Word模板时,Word内容循环问题
我做了一个Word模板,用ASP将ACCESS表中查询到的值赋给一个个变量将其输出到Word模板里.里面有些内容查询出来是多个,需要循环,但不知怎么弄.请高手指点,非常感谢!

我有两个文件:一个是.asp的,调用数据和打word模板的;另一个是word模板.

在.asp文件中,相关的语句是:

<%
'创建文件
dim templateName,templatechar,filepath,filename,fileCharset,templateContent
templateName="template/template_word.htm" '模板名字
templatechar="gb2312" '模板文本的编码
filepath="files/word/" '生成文件保存的路径
filename="Template.doc" '即将生成的文件名
CreateMultiFolder(filepath) '这一句用来判断文件夹是否存在
fileCharset="gb2312" '打算生成的文本编码

'读取指定的模板内容
templateContent=ReadFromTextFile(templateName,templatechar)
'以下是用来替换模板内容的
templateContent=replace(templateContent,"{$tID}",TD.Fields.Item("tID").Value)

templateContent=replace(templateContent,"{$tecompany}",TDep.Fields.Item("tecompany").Value)

templateContent=replace(templateContent,"{$tothers}",TDoh.Fields.Item("tothers").Value)

'最终调用函数来生成文件
Call WriteToTextFile(filepath&filename,templateContent,fileCharset)

'最后关闭adodb.stream对象
stm.flush
stm.Close
set stm=nothing
downloadFile(filepath&filename)
%>

我要循环的是:
templateContent=replace(templateContent,"{$tecompany}",TDep.Fields.Item("tecompany").Value)

我尝试过几种方法,但都不行,请高手指点,非常感谢! Asp ?Word模板 循环
[解决办法]
替换前你就先组合游标中的TDep.Fields.Item("tecompany").Value成一个字符串,然后再替换就行了,要不第一次就替换掉{$tecompany},后续的就无法再加入其他的TDep.Fields.Item("tecompany").Value了

要不可以改成下面这样的
templateContent=replace(templateContent,"{$tecompany}",TDep.Fields.Item("tecompany").Value&"{$tecompany}")

可以重复替换,但是最后循环完后要多替换一次,将{$tecompany}替换为空

'创建文件
dim templateName,templatechar,filepath,filename,fileCharset,templateContent
templateName="template/template_word.htm" '模板名字
templatechar="gb2312" '模板文本的编码
filepath="files/word/" '生成文件保存的路径
filename="Template.doc" '即将生成的文件名
CreateMultiFolder(filepath) '这一句用来判断文件夹是否存在
fileCharset="gb2312" '打算生成的文本编码



'读取指定的模板内容
templateContent=ReadFromTextFile(templateName,templatechar)


while not TDep.eof'''''''''''''''
'以下是用来替换模板内容的
templateContent=replace(templateContent,"{$tID}",TD.Fields.Item("tID").Value)

templateContent=replace(templateContent,"{$tecompany}",TDep.Fields.Item("tecompany").Value&"{$tecompany}")'''''''

templateContent=replace(templateContent,"{$tothers}",TDoh.Fields.Item("tothers").Value)

TDep.movenext''''''''''''
wend'''''''''''''''''

templateContent=replace(templateContent,"{$tecompany}","")'''''''


'最终调用函数来生成文件
Call WriteToTextFile(filepath&filename,templateContent,fileCharset)

'最后关闭adodb.stream对象
stm.flush
stm.Close
set stm=nothing
downloadFile(filepath&filename)
%>


[解决办法]
那你只好先组合成字符串,再一次性替换了,并且"tecompany" "product" "price"只替换一次,其他另外2个替换为空,或者修改你的模板只保留{$内容}替换"tecompany" "product" "price"这2个就行了

tID=TD.Fields.Item("tID").Value'''''''''''
tothers=TDoh.Fields.Item("tothers").Value'''''''


content="<table>"
while not TDep.eof'''''''''''''''
content=content&"<tr><td>"&TDep.Fields.Item("tecompany").Value&"</td><td>"&TDep.Fields.Item("product").Value&"</td><td>"&TDep.Fields.Item("price").Value&"</td></tr>"
TDep.movenext''''''''''''
wend'''''''''''''''''
content=content&"</table>"''''''''


templateContent=replace(templateContent,"{$tID}",tID)
templateContent=replace(templateContent,"{$tecompany}",content)'''''''替换为内容
templateContent=replace(templateContent,"{$product}","")'''''''替换为空
templateContent=replace(templateContent,"{$price}","")'''''''替换为空
templateContent=replace(templateContent,"{$tothers}",TDoh.Fields.Item("tothers").Value)

读书人网 >ASP

热点推荐