用户登录
用户注册

分享至

如何创建Word文件?

  • 作者: 要上我随便
  • 来源: 51数据库
  • 2020-08-08

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

------------------------------------------------------------------------------------------------------------------------------------

 

 

 

软件
前端设计
程序设计
Java相关