如何使用 ASP 来生成 RTF 格式 (RTF) 文档到流到 MicrosoftWord

Support for Office 2003 has ended

Microsoft ended support for Office 2003 on April 8, 2014. This change has affected your software updates and security options. Learn what this means for you and how to stay protected.

注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完成。微软很高兴能同时提供给您由人工翻译的和由机器翻译的文章, 以使您能使用您的语言访问所有的知识库文章。然而由机器翻译的文章并不总是完美的。它可能存在词汇,语法或文法的问题,就像是一个外国人在说中文时总是可能犯这样的错误。虽然我们经常升级机器翻译软件以提高翻译质量,但是我们不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的错误使用所引起的任何直接的, 或间接的可能的问题负责。如果您发现了错误并希望帮助我们提高机器翻译技术,请完成文章末尾的在线调查。

点击这里察看该文章的英文版: 270906
概要
本文介绍如何生成与 ASP 脚本 Rich Text Format (RTF) 文件, 然后流 MicrosoftWord 这些文件。 此方法提供替代到服务器端自动化是 Microsoft Word 用于运行时文档生成。

注意 本文要求您具有份 Northwind.mdb 示例数据库从 Microsoft Access 2000, 从 Microsoft Access 2002, 或 Microsoft Office Access 2003 中安装。
更多信息
要生成 Word 文档以 ASP 服务器端缺点有:
  • 性能 MicrosoftWord 是进程外自动化服务器 ; 进程外调用花费较长时间来执行。

  • 伸缩性存为 " 自动化服务器, MicrosoftWord 具有大量开销因为为了可 reap Word 的丰富对象模型的优点必须加载 Winword.exe 文件。
通过从头也不使用 Word 对象模型, 生成文档您可使 ASP 应用程序更可伸缩而且还提高性能。 方法用于示例, 演示如何使用 可以在 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 站点:

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 知识库文章中文章:
257757 服务器端自动化 Office 的考虑事项
193998 如何读取和显示 ASP 中二进制数据
266263 当使用 MIME 类型到流数据 BUG: Word 2000 和 Excel 2000 显示 ASP 源
247318 BUG: Word 2000 和 Excel 2000 执行不定向正确使用 Response.Redirect 时
richtext rich-text WD2007

属性

文章 ID:270906 - 上次审阅时间:04/19/2007 18:37:17 - 修订版本: 8.0

Microsoft Office Word 2007, Microsoft Office Word 2003, Microsoft Word 2000 Standard Edition, Microsoft Word 97 Standard Edition, Microsoft Active Server Pages 4.0

  • kbexpertiseinter kbautomation kbfso kbhowto kbprogramming KB270906 KbMtzh kbmt
反馈