|
![]() |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
开始时,将所有的网页表单域读入接收网页的隐含表单域中。在下面的源代码中,请注重在Body标记中对“onLoad”的调用。它调用buildDoc VB脚本子程序,向它传递3个参数:页面中表单的内容(所有的隐含域)、Word模板文件的位置、从输入表单中收到的行数。读所有的输入表单域,然后当页面装载后调用buildDoc子程序。为了简短起见,这里假定所有变量在使用之前都已被声明: buildDoc.asp中装载输入表单域的代码如下: <HEAD> <TITLE>Build Document</TITLE> <META HTTP-EQUIV="Refresh" CONTENT="30;URL=\\\'orderForm.asp\\\'"> </HEAD> <% dotLocation="\\\'servernamedirectory heTemplate.dot\\\'" intRowCount = Request.Form("rowCount") \\\'initialize a row counter %> <BODY Language="VBScript" onLoad="buildDoc document.theForm, <%=dotLocation%>,intRowCount> <FORM NAME="theForm"> <% itemCount = 0 \\\'set field counter to zero For Each Item in Request.Form \\\'count up the form fields itemCount = itemCount + 1 \\\'using For..Next loop %> <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) %> 现在开始检查所有的表单行。在所有输入的网页表单域中循环,收集每个表单域名及其相应的值。逐个检查以决定将其放入哪个数列元素内,然后将其放入。以下举例代码中的SELECT CASE命令很重要,这决定表单域属于哪一列。为了方便,使用不确定编码的CASE选择。 <% 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那样难。 <% For j = 1 to intTableRows 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)) 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 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 %> 希望对你做一样的工作能有一些帮助。我可以肯定需要从网页表单中创建文件的不止我自己。 这是我的方式。假如你有更好的方式,我很愿意学习。 返回类别: 教程 上一教程: 抓取动网论坛EMAIL地址的一段代码 下一教程: 用ASP+XML打造留言本(1) 您可以阅读与"用ASP创建MICROSOFT WORD文件"相关的教程: · 用ASP创建WORD文件 · 用ASP创建日志文件(附源程序) · 用ASP和MICROSOFT.XMLDOM分析远程XML文件 · 在ASP中自动创建多级文件夹的函数 · 用ASP创建日志文件(附源程序) |
![]() ![]() |
快精灵印艺坊 版权所有 |
首页![]() ![]() ![]() ![]() ![]() ![]() ![]() |