日期:2014-05-16  浏览次数:20993 次

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"