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
%>
------------------------------------------------------------------------------------------------------------------------------------