随着人们对数据库及动态HTML (DHTML)的应用和对XML的兴趣的增加,所以在商业网站的数据中使用动态内容变得越来越普遍。这里我们就成功运用了一则最新的技巧——用ASP创建 Word 文件,我想您看后肯定会受到很大的启发。
原文出处:http://www.asptoday.com/articles/19990825.htm
〈%
For intCount = 0 to frmData.fieldCount.value
strOkay = "Y"
strSearch = frmData.elements(intCount).name 'load the field name
strValue = frmData.elements(intCount).value 'load the field value
strPosition = Instr(1,strSearch,"_") 'get pos val of "_"
intStringLen=strPosition-1
If intStrLen > 0 Then
strLeft = Left(strSearch,intStringLen)
strRight = Right(strSearch,(Len(strSearch)-Len(strLeft)-1))
Select Case strLeft
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
%>
现在开始创建文件。对于激活的文件,用变量rngCurrent设置Microsoft Word文件对象RANGE(为了防止用户打开另一个文件),通过指定表格的位置( rngCurrent)以及行、列的数目来确定其大小。
〈%
Set rngCurrent = objWordDoc.Application.ActiveDocument.Content
Set tabCurrent = ObjWordDoc.Application.ActiveDocument.Tables.Add
rngCurrent,intNumrows,4)
%>
创建了有表格的文件之后,我们开始往表格中装入数据。首先指到第一行row(tabRow=1 ), 然后进行逐行循环。在每行结尾处插入回车[Chr(10)],以便产生行间空行,最后增加行计数器,用“FormatCurrency” 输出美圆值以保证使用$符号、逗号、小数点的位置。通过在
“ParagraphAlignment=2”处设置栏数来实现美圆数量的正确调整。用VBA容易一些,不象用VBScript那样难。
〈%
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
%>
希望对你做相同的工作能有一些帮助。