有谁知道什么实现生成WORD?

wyp0623 2004-10-08 04:34:10
我想实现一个能生成WORD文件的系统,而且WORD最好是模板式的,生成的WORD格式一样,只是内容不同.
想以向导式的型式实现,但不知什么实现,与数据庫要不要联系呢
...全文
430 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzsdream 2004-10-20
  • 打赏
  • 举报
回复
背景

   BuildDoc.asp是一个ASP文件,它读入一个网页表单的输出,并创建一个Microsoft Word文件作为输出, 其中包含根据表单内数据改变产生的一个表格。表单内容不再局限于静态信息。也就是说, 表单中所显示的内容可能随着用户的交互作用而改变。



  
   BuildDoc所满足的商业需求是:根据销售人员网页列表变化的记录,建立表单信件。只有被销售人员修改过的数据才被发送到Word, 在那里这些数据被格式化到表格中。

   BuildDoc读入表单上的所有信息,识别被改变的行,然后用被改变的行中包含的信息来创建一个 Microsoft Word文件。BuildDoc使用一个模板文件(buildDoc.dot),其中包含地址头和一些预先格式化的文本。然后向文件中写入一个表格,其中的每一行都对应与网页表单中被修改过的一行。



  
   怎么做?

   开始时,将所有的网页表单域读入接收网页的隐含表单域中。在下面的源代码中,请注意在Body标记中对“onLoad”的调用。它调用buildDoc VB脚本子程序,向它传递3个参数:页面中表单的内容(所有的隐含域)、Word模板文件的位置、从输入表单中收到的行数。读所有的输入表单域,然后当页面装载后调用buildDoc子程序。为了简短起见,这里假定所有变量在使用之前都已被声明:

   buildDoc.asp中装载输入表单域的代码如下:


〈!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="'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
%>
lqflsh 2004-10-20
  • 打赏
  • 举报
回复
跟分没有关系,csdn的人要是看分的话,也没啥意思,还不如弄个传奇呢!

这种情况似乎要用到控件,不然不是很好处理
http://www.ntko.com/
ljqhbt 2004-10-15
  • 打赏
  • 举报
回复
<input type="hidden" name="out_word" onclick="vbscript:buildDoc" value="导出到word" class="notPrint">
<input type="hidden" name="out_excel" onclick="AutomateExcel();" value="导出到excel" class="notPrint">

<title>浏览器表格导出到Excel代码</title>
<input type="button" name="out_word" onclick="vbscript:buildDoc" value="导出到word" class="notPrint">
<input type="button" name="out_word1" onclick="javascript:AutomateExcel() " value="导出到excel" class="notPrint">


<table id="data" width="200" border="1">
<tr>
<td>11</td>
<td>11</td>
</tr>
<tr>
<td>22</td>
<td>22</td>
</tr>
<tr>
<td>33</td>
<td>33</td>
</tr>
<tr>
<td>44 </td>
<td>44</td>
</tr>
</table>


<SCRIPT LANGUAGE="JavaScript">
<!--
function AutomateExcel()
{
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var table = document.all.data;
var hang = table.rows.length;

var lie = table.rows(0).cells.length;

// Add table headers going cell by cell.
for (i=0;i<hang;i++)
{
for (j=0;j<lie;j++)
{
oSheet.Cells(i+1,j+1).Value = table.rows(i).cells(j).innerText;
}

}
oXL.Visible = true;
oXL.UserControl = true;
}
//-->
</SCRIPT>

导出到Word代码
<script language="vbscript">
Sub buildDoc
set table = document.all.data
row = table.rows.length
column = table.rows(1).cells.length

Set objWordDoc = CreateObject("Word.Document")

'objWordDoc.Application.Documents.Add theTemplate, False
objWordDoc.Application.Visible=True

Dim theArray(20,10000)
for i=0 to row-1
for j=0 to column-1
theArray(j+1,i+1) = table.rows(i).cells(j).innerTEXT
next
next
objWordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("综合查询结果集") //显示表格标题

objWordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("")
Set rngPara = objWordDoc.Application.ActiveDocument.Paragraphs(1).Range
With rngPara
.Bold = True //将标题设为粗体
.ParagraphFormat.Alignment = 1 //将标题居中
.Font.Name = "隶书" //设定标题字体
.Font.Size = 18 //设定标题字体大小
End With
Set rngCurrent = objWordDoc.Application.ActiveDocument.Paragraphs(3).Range
Set tabCurrent = ObjWordDoc.Application.ActiveDocument.Tables.Add(rngCurrent,row,column)

for i = 1 to column

objWordDoc.Application.ActiveDocument.Tables(1).Rows(1).Cells(i).Range.InsertAfter theArray(i,1)
objWordDoc.Application.ActiveDocument.Tables(1).Rows(1).Cells(i).Range.ParagraphFormat.alignment=1
next
For i =1 to column
For j = 2 to row
objWordDoc.Application.ActiveDocument.Tables(1).Rows(j).Cells(i).Range.InsertAfter theArray(i,j)
objWordDoc.Application.ActiveDocument.Tables(1).Rows(j).Cells(i).Range.ParagraphFormat.alignment=1
Next
Next

End Sub
</SCRIPT>
wyp0623 2004-10-15
  • 打赏
  • 举报
回复
或许哪位有更好的办法呢,帮帮忙啊,我都晕死了
wyp0623 2004-10-15
  • 打赏
  • 举报
回复
小妹在此谢谢各位了,
又有一个问题:
什么实现对word进行查询呢
比如我查某个章节名,就只把这章节的内容提取出来呢
明珠佩佩 2004-10-14
  • 打赏
  • 举报
回复
asp处理 word文档经常遇到权限问题,参考
http://support.microsoft.com/default.aspx?scid=kb;zh-cn;288366
http://support.microsoft.com/kb/288367/EN-US/
http://support.microsoft.com/kb/288367/zh-cn

帮你找的,看看行不??
明珠佩佩 2004-10-14
  • 打赏
  • 举报
回复
Option Explicit

'declare all variables
Dim objWord
Dim oDoc
Dim objFso
Dim colFiles
Dim curFile
Dim curFileName
Dim folderToScanExists
Dim folderToSaveExists
Dim objFolderToScan

'set some of the variables
folderToScanExists = False
folderToSaveExists = False
Const wdSaveFormat = 10 'for Filtered HTML output

'********************************************
'change the following to fit your system
Const folderToScan = "C:\Word\documentation\"
Const folderToSave = "C:\Inetpub\wwwroot\word\"
'********************************************

'Use FSO to see if the folders to read from
'and write to both exist.
'If they do, then set both flags to TRUE,
'and proceed with the function
Set objFso = CreateObject("Scripting.FileSystemObject")
If objFso.FolderExists(folderToScan) Then
folderToScanExists = True
Else
MsgBox "Folder to scan from does not exist!", 48, "File System Error"
End If
If objFso.FolderExists(folderToSave) Then
folderToSaveExists = True
Else
MsgBox "Folder to copy to does not exist!", 48, "File System Error"
End If

If (folderToScanExists And folderToSaveExists) Then
'get your folder to scan
Set objFolderToScan = objFso.GetFolder(folderToScan)
'put al the files under it in a collection
Set colFiles = objFolderToScan.Files
'create an instance of Word
Set objWord = CreateObject("Word.Application")
If objWord Is Nothing Then
MsgBox "Couldn't start Word.", 48, "Application Start Error"
Else
'for each file
For Each curFile in colFiles
'only if the file is of type DOC
If (objFso.GetExtensionName(curFile) = "doc") Then
'get the filename without extension
curFileName = curFile.Name
curFileName = Mid(curFileName, 1, InStrRev(curFileName, ".") - 1)
'open the file inside Word
objWord.Documents.Open objFso.GetAbsolutePathName(curFile)
'do all this in the background
objWord.Visible = False
'create a new document and save it as Filtered HTML
Set oDoc = objWord.ActiveDocument
oDoc.SaveAs folderToSave & curFileName & ".htm", wdSaveFormat
oDoc.Close
Set oDoc = Nothing
End If
Next
End If
'close Word
objWord.Quit
'set all objects and collections to nothing
Set objWord = Nothing
Set colFiles = Nothing
Set objFolderToScan = Nothing
End If

Set objFso = Nothing
xuanhanxian 2004-10-14
  • 打赏
  • 举报
回复
输出内容的指定方法:1.文本文件:response.contenttype="text/plain"
2.word文档:response.contenttype="application/msword"
3.图片:response.contenttype="image/gif"
serverme 2004-10-14
  • 打赏
  • 举报
回复
谁能搞定得话我帮楼主给分
mikespook 2004-10-14
  • 打赏
  • 举报
回复
跟分没关系,是这个问题有点偏门~~~

兔子已经给出了解决方法,具体要插入什么内容,那就需要你自己写了。有个窍门,可以打开一个WORD文档录制宏,然后修改一下宏,把代码复制出来放到ASP里就可以了。
wyp0623 2004-10-12
  • 打赏
  • 举报
回复
不会吧,我没多少分了,没办法那就多加分了
flame2000 2004-10-11
  • 打赏
  • 举报
回复
gz
wyp0623 2004-10-11
  • 打赏
  • 举报
回复
<%Set objWordApp = CreateObject("Word.Application")
Set objWordDoc = objWordApp.Documents.Add
objWordApp.Visible = True
Set objWordApp = Nothing

Set objParagraph = objWordDoc.Paragraphs.Add
Set objRange = objParagraph.Range
With objRange
.InsertBefore "哈哈哈"
.ParagraphFormat.Alignment = 1
.Bold = True
.Font.Name = "隶书" //设定标题字体
.Font.Size = 15
End With
Set objRange = Nothing
Set objParagraph = Nothing

Set objParagraph = objWordDoc.Paragraphs.Add
Set objRange = objParagraph.Range
objRange.InsertBefore ("")
Set objTable = objWordDoc.Tables.Add(objRange, row, col)
Set objRange = Nothing
Set objParagraph = Nothing
%>


Microsoft VBScript 编译器错误 错误 '800a03f6'

缺少 'End'

/iisHelp/common/500-100.asp,行242 错误 '80010105'

服务器出现意外情况。

帮看看
hksfans_MA 2004-10-11
  • 打赏
  • 举报
回复
你的分太少了啊,有些人不会来的,不过兔子还是来了,亲爱的兔子啊。
wyp0623 2004-10-11
  • 打赏
  • 举报
回复
要不换成:

原始有一个Excel,里有多个sheet,sheet有多个表,这些表都是有关联的(即值是由其它几个单元用公式算出的),现想与Web联合,用Web形式输入一些数据到Excel中的某些单元中,Excel自动计算其它值,后生成出各个表(这表不能留有公式,只单单是值),供前台打印、或复制形式粘贴到WORD中等方式,什么实现
jim.ma 2004-10-10
  • 打赏
  • 举报
回复
关注 帮你顶 ................
wyp0623 2004-10-10
  • 打赏
  • 举报
回复
大家能不能具体点啊
patchclass 2004-10-10
  • 打赏
  • 举报
回复
Set App = CreateObject("Word.Application")
wyp0623 2004-10-10
  • 打赏
  • 举报
回复
有谁会知道的啊,有没现成的代码啊
tiandiqing 2004-10-10
  • 打赏
  • 举报
回复
这要用到vba编程了,
这东西一直是个问题,通常在OA中公文所用比较多,如果自己有能力可以写个控建就好实现了

去网上查查吧
加载更多回复(10)

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧