你好,欢迎来到电脑编程技巧与维护杂志社! 杂志社简介广告服务读者反馈编程社区  
合订本订阅
 
 
您的位置:技术专栏 / Web开发
如何使用 ASP 来生成 RTF 格式 (RTF) 文档到流到 MicrosoftWord
 

概要

本文阐释如何生成 RTF 格式 (RTF) 文件与 ASP 脚本, 然后传输这些文件到 MicrosoftWord 此方法提供替代到服务器端自动化是 Microsoft Word 用于运行时文档生成。

更多信息

要生成 Word 文档以 ASP 服务器端缺点有:

性能 MicrosoftWord 是进程外自动化服务器 进程外调用花费较长时间来执行。

伸缩性存为 " 自动化服务器, MicrosoftWord 具有大量开销因为为了可 reap Word 的丰富对象模型的优点必须加载 Winword.exe 文件。

通过从头也不使用 Word 对象模型, 生成文档您可使 ASP 应用程序更可伸缩而且还提高性能。 方法用于示例, 演示如何使用 FileSystemObject 可以在 MicrosoftWord 中创建 RTF 文件可以解释浏览客户上。

首示例说明如何使用 ASP 来创建 RTF 文档包含页眉和页脚、 包含表, 并用于文本整个文档使用不同字体样式和颜色。 第一个示例使用 ADO 来从示例 Northwind 数据库以创建一个 90 plus - - 页文档访问数据。


第二个示例演示如何创建 RTF 文档类似与 Word 邮件合并。 结果文档包含页眉和页脚、 各种段落格式和分页, 和使用不同字体样式和类型整个文档。 此示例还使用 ADO 来访问数据从 Northwind 数据库以创建一个 170 plus - - 页文档。


注意 RTF 格式 (RTF) 规范是公用规范以生成 RTF - 兼容文本文件。 您对以下 Microsoft Developer Network (MSDN) Web 站点上规范作为资源使用文档以帮助您构建自己的 RTF 文件。 但是, 规范提供 " 作为是, 并且是不 supportted Microsoft 技术支持 - " 有关最新版本的 RTF 规范, 请访问以下 MSDNWeb 站点:

Rich Text Format (RTF) Specification 1.6
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnrtfspec/html/rtfspec.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnrtfspec/html/rtfspec.asp)

ASP 示例 1

下列 ASP 说明创建包含报表的计算数字 90 plus - - RTF 文档。

注意 变量 sConn 此代码示例中包含路径到 Northwind 数据库。 确认是否适合您的 Office 安装提供路径。 您还需要提供对以便可以创建该文档包含 ASP 文件夹的 写入 访问。 此访问应该限于 WindowsNT , 下特定个人但是能应用于 Everyone 用于匿名访问。

<%@ Language=VBScript %> <% Dim sRTF, sFileName, sConn sConn = "c:\program files\microsoft office\office\samples\northwind.mdb" Response.Buffer = True 'Create the file for the RTF Dim fso, MyFile Set fso = CreateObject("Scripting.FileSystemObject") sFileName = "sample1.doc" Set MyFile = fso.CreateTextFile(Server.MapPath(".") & "\" & _ sFileName, True) MyFile.WriteLine("{\rtf1") 'Write the color table (for use in background and foreground colors) sRTF = "{\colortbl;\red0\green0\blue0;\red0\green0\blue255;" & _ "\red0\green255\blue255;\red0\green255\blue0;" & _ "\red255\green0\blue255;\red255\green0\blue0;" & _ "\red255\green255\blue0;\red255\green255\blue255;}" MyFile.WriteLine(sRTF) 'Write the title and author for the document properties MyFile.WriteLine("{\info{\title Sample RTF Document}" & _ "{\author Microsoft Developer Support}}") 'Write the page header and footer MyFile.WriteLine("{\header\pard\qc{\fs50 " & _ "ASP-Generated RTF\par}{\fs18\chdate\par}\par\par}") MyFile.WriteLine("{\footer\pard\qc\brdrt\brdrs\brdrw10\brsp100" & _ "\fs18 Page " & _ "{\field{\*\fldinst PAGE}{\fldrslt 1}} of " & _ "{\field{\*\fldinst NUMPAGES}{\fldrslt 1}} \par}") 'Write a sentence in the first paragraph of the document MyFile.WriteLine("\par\fs24\cf2 This is a sample \b RTF \b0 " & _ "document created with ASP.\cf0") 'Connect to the database in read-only mode Dim conn, rs Set conn = Server.CreateObject("ADODB.Connection") conn.Provider = "Microsoft.Jet.OLEDB.4.0" conn.Mode = 1 'adModeRead=1 conn.Open sConn 'Execute a query that returns ID, Product name and amount of sale Set rs = Server.CreateObject("ADODB.Recordset") rs.Open "SELECT [Order Details].OrderID AS ID, " & _ "Products.ProductName AS Name, " & _ "[Order Details].[UnitPrice]*[Quantity] AS Amount " & _ "FROM Products INNER JOIN [Order Details] ON " & _ "Products.ProductID = [Order Details].ProductID " & _ "ORDER BY [Order Details].OrderID", conn, 3 'adOpenStatic = 3 MyFile.WriteLine("{") 'Start table MyFile.WriteLine("\par\par\fs24") 'Write the table header row (appears at top of table on each page) sRTF = "\trowd\trhdr\trgaph30\trleft0\trrh262" & _ "\cellx2000\cellx7000\cellx9000" & _ "\pard\intbl\qc\b\i\ul Order ID \ul0\i0\b0\par\cell" & _ "\pard\intbl\ql\b\i\ul Product \ul0\i0\b0\par\cell" & _ "\pard\intbl\qr\ul\b\i Amount \ul0\i0\b0\par\cell" & _ "\pard\intbl\row" MyFile.WriteLine(sRTF) dim LastID dim CurID dim CurTotal dim bFirstRow bFirstRow=true do while not (rs.eof) if LastID<>rs("ID").Value and not(bFirstRow) Then 'Starting on a row for a different id, so add the last total and 'then a blank row sRTF = "\trowd\trgaph30\trleft0\trrh262" & _ "\cellx2000\cellx7000\clbrdrb\brdrdb\cellx9000" & _ "\pard\intbl\cell\pard\intbl\cell\pard\intbl\qr\b " & _ FormatCurrency(CurTotal, 0, False, False, True) & _ "\b0\cell\pard\intbl\row" & _ "\trowd\trgaph30\trleft0\trrh262" & _ "\cellx2000\cellx7000\cellx9000" & _ "\pard\intbl\cell\pard\intbl\cell\pard\intbl\cell" & _ "\pard\intbl\row" MyFile.WriteLine(sRTF) CurID = rs("ID").Value CurTotal = 0 elseif bFirstRow then CurID = rs("ID").Value else CurID = "" end if 'Add a new row with the ID, the Product name and the amount 'Note: Amounts over 1000 are formatted with red. sRTF = "\trowd\trgaph30\trleft0\trrh262" & _ "\cellx2000\cellx7000\cellx9000" & _ "\pard\intbl\qc " & CurID & "\cell" & _ "\pard\intbl\ql " & rs("Name").Value & "\cell" If rs("Amount").Value >1000 Then sRTF = sRTF & "\pard\intbl\qr\cf6 " & _ FormatNumber(rs("Amount").Value, 0, False, False, True) & _ "\cf0\cell\pard\intbl\row" else sRTF = sRTF & "\pard\intbl\qr " & _ FormatNumber(rs("Amount").Value, 0, False, False, True) & _ "\cell\pard\intbl\row" end if MyFile.WriteLine(sRTF) LastID = rs("ID").Value CurTotal = CurTotal + rs("Amount").Value rs.MoveNext bFirstRow=false loop MyFile.WriteLine("}") 'End Table 'Add a page break and then a new paragraph MyFile.WriteLine("\par \page") MyFile.WriteLine("\pard\fs18\cf2\qc " & _ "This sample provided by Microsoft Developer Support.") 'Close the recordset and database rs.Close conn.Close Set rs = Nothing Set conn = Nothing 'close the RTF string and file MyFile.WriteLine("}") MyFile.Close Response.Write _ "<META HTTP-EQUIV=""REFRESH"" Content=""0;URL=" & sFileName & """>" %>

ASP 示例 2

下列 ASP 演示模拟对 Word 邮件合并或套用信函。 ADO 记录中记录生成 170 plus - - 页文档中每页。

注意 变量 sConn 此代码示例中包含路径到 Northwind 数据库。 确认是否适合您的 Office 安装提供路径。 您还需要提供对因此文档可以创建包含 ASP 文件夹的 写入 访问。 此访问应该限于 NT , 下特定个人但是能应用于 Everyone 用于匿名访问。

<%@ Language=VBScript %> <% Dim sRTF, sConn sConn = "c:\program files\microsoft office\office\samples\northwind.mdb" Response.Buffer = True 'Create the file for the RTF Dim fso, MyFile, sFileName Set fso = CreateObject("Scripting.FileSystemObject") sFileName = "sample2.doc" Set MyFile = fso.CreateTextFile(Server.MapPath(".") & "\" & _ sFileName, True) MyFile.WriteLine("{\rtf1") 'Write the font table sRTF = "{\fonttbl {\f0\froman\fcharset0 Times New Roman;}" & _ "{\f1\fswiss\fcharset0 Arial;}" & _ "{\f2\fmodern\fcharset0 Courier New;}}" MyFile.WriteLine sRTF 'Write the title and author for the document properties MyFile.WriteLine("{\info{\title Sample RTF Document}" & _ "{\author Microsoft Developer Support}}") 'Write the document header and footer MyFile.WriteLine("{\header\pard\qc\brdrb\brdrs\brdrw10\brsp100" & _ "{\fs50 Northwind Traders} \par}") MyFile.WriteLine("{\footer\pard\qc\brdrt\brdrs\brdrw10\brsp100" & _ "{\fs18 Questions?\par Call (111)-222-3333, " & _ " Monday-Friday between 8:00 am and 5:00 pm} \par}") 'Connect to the database in read-only mode Dim conn, rs Set conn = Server.CreateObject("ADODB.Connection") conn.Provider = "Microsoft.Jet.OLEDB.4.0" conn.Mode = 1 'adModeRead=1 conn.Open sConn 'Execute a query that returns ID, Product name and amount of sale Set rs = Server.CreateObject("ADODB.Recordset") rs.Open "SELECT Orders.ShippedDate, Orders.ShipVia, " & _ "Orders.OrderDate, Orders.OrderID, " & _ "Customers.ContactName FROM Customers INNER JOIN Orders ON " & _ "Customers.CustomerID = " & _ "Orders.CustomerID WHERE (((Orders.ShippedDate) Between " & _ "#1/1/1998# And #3/31/98#))",conn,3 'adOpenStatic = 3 Do While Not (rs.eof) 'Write the "body" of the form letter MyFile.WriteLine "\fs26\f1" 'Default font MyFile.WriteLine "\pard" MyFile.WriteLine "\par\par\par\par" MyFile.WriteLine "\par RE: Order #" & rs.Fields("OrderID").Value MyFile.WriteLine "\par" MyFile.WriteLine "\par" MyFile.WriteLine "\par " & rs.Fields("ContactName").Value & ", " MyFile.WriteLine "\par" MyFile.WriteLine "\par Thank you for your order on " & _ rs.Fields("ShippedDate").Value & _ ". Your order has been shipped: " MyFile.WriteLine "\par" MyFile.WriteLine "\par" MyFile.WriteLine "\pard \li720 {\f2" MyFile.WriteLine "\par \b Order Number \b0 \tab " & _ rs.Fields("OrderID").Value MyFile.WriteLine "\par \b Shipped By \b0 \tab " & _ rs.Fields("ShipVia").Value MyFile.WriteLine "\par \b Shipped On \b0 \tab " & _ rs.Fields("ShippedDate").Value MyFile.WriteLine "\par}" MyFile.WriteLine "\pard" MyFile.WriteLine "\par" MyFile.WriteLine "\par Northwind Traders is committed to " & _ "bringing you products of the " & _ "highest quality from all over the world. If " & _ "at any time you are not completely satisfied " & _ "with any of our products, you may " & _ "return them to us for a full refund." MyFile.WriteLine "\par" MyFile.WriteLine "\pard {\fs18 \qc \i" MyFile.WriteLine "\par Thank you for choosing Northwind Traders!" MyFile.WriteLine "\par}" rs.MoveNext 'Add a page break and then a new paragraph If Not(rs.eof) Then MyFile.WriteLine("\pard \page") Loop 'Close the recordset and database rs.Close conn.Close Set rs = Nothing Set conn = Nothing 'close the RTF string and file MyFile.WriteLine("}") MyFile.Close Response.Write _ "<META HTTP-EQUIV=""REFRESH"" Content=""0;URL=" & sFileName & """>" %>

 

这篇文章中的信息适用于:

Microsoft Office Word 2003

Microsoft Word 2000 Standard Edition

Microsoft Word 97 Standard Edition

Microsoft Active Server Pages 4.0

                                             (Alex   转自MSDN)

  推荐精品文章

·2024年2月目录 
·2024年1月目录
·2023年12月目录
·2023年11月目录
·2023年10月目录
·2023年9月目录 
·2023年8月目录 
·2023年7月目录
·2023年6月目录 
·2023年5月目录
·2023年4月目录 
·2023年3月目录 
·2023年2月目录 
·2023年1月目录 

  联系方式
TEL:010-82561037
Fax: 010-82561614
QQ: 100164630
Mail:gaojian@comprg.com.cn

  友情链接
 
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved
京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089