用ASP建立Word檔案

來源:互聯網
上載者:User
word|建立|word Using Active Server Pages to Build Microsoft Word Documents  
By Gardiner B. Jones  

Background  
BuildDoc.asp is an Active Server Page (ASP) that reads the output of a

Web page form, and creates as output a Microsoft Word document contai

ning a table of changed data within the form. Forms are no longer limi

ted to containing static information. With database connectivity, the  

increasing use of Dynamic HTML (DHTML), and the growing interest in XM

L, it has become common practice in business Web pages for the data co

ntained in them to be dynamic. That is, what is shown in the form may  

change based on user interaction (see the sample input form below).  



The business need filled by BuildDoc is to enable sales associates to  

create form letters from the changed records of a Web page table. Only

the data modified by the sales person is sent to Word, where it is fo

rmatted into a table. Obviously, all samples here are fictitious.  

BuildDoc will read all of the information on the form, identifying whi

ch rows have been changed, and then creates the Microsoft Word documen

t using only the information contained within the changed rows (see th

e sample output document below). BuildDoc uses a template file (buildD

oc.dot) that contains the address header, and some preformatted text.  

It then writes a table into the document that has a row for each modif

ied row from the Web page form.  



How To Do It  
We start by reading all of the Web page form fields into hidden form f

ields on the receiving Web page. In the source code below, note the "  

onLoad " call in the body tag. It calls the buildDoc VBScript subrouti

ne, passing three parameters to it: the contents of the page's form (a

ll the hidden fields), the location of the Word template file, and the

number of rows received from the input form. The input form fields ar

e all read and then, when the page loads, it calls the buildDoc subrou

tine. For the sake of brevity, we will assume that all variables have  

been first declared before use.  

The code for the loading of the input form fields into buildDoc.asp is

thus: -  

<!DOCTYPE HTML PUBLIC "-//W3C/DTD HTML 3.2 Final//EN">  
<HEAD>  
<TITLE>Build Document</TITLE>  
<META HTTP-EQUIV="Refresh" CONTENT="30;URL='orderForm.asp'">  
</HEAD>  
<%
dotLocation="'\\servername\directory\theTemplate.dot'"
intRowCount = Request.Form("rowCount") 'initialize a row counter
%>  
<BODY Language="VBScript" 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>  
We create an instance of the Word Document object, using the sample co

de immediately below. Note that in Internet Explorer 4+ this will fail

unless the browser security is set to Low, or Custom with the appropr

iate setting to run programs.  

<%
Set objWordDoc = CreateObject("Word.Document")
ObjWordDoc.Application.Documents.Add theTemplate, False
ObjWordDoc.Application.Visible=True
%>  
We re-dimension our array so that it is the same size as the number of

rows that are contained in the Web page's form. In this case, we set  

the Y-axis to a constant value of four because that is the number of c

olumns we need in the output document. The X-axis contains the number  

of received rows from the form.  

<%
Redim Preserve theArray(4,intTableRows)
%>  
Now we are ready to examine all of the form rows. We do this by loopin

g through all the input Web page form fields to collect each form fiel

d name and corresponding value. We test each to determine which array  

element to put it into, and then we put it there. The SELECT CASE stat

ement in the code sample



相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。