บทความนี้อธิบายวิธีการสร้างแฟ้มการจัดรูปแบบข้อความ Rich (RTF) กับสคริปต์ ASP และแฟ้มเหล่านั้นไปยัง Microsoft Word ที่ส่งกระแสข้อมูลแล้ว เทคนิคนี้แสดงแทนฝั่งเซิร์ฟเวอร์ Automation ของ Microsoft Word สำหรับการสร้างเวลาทำงานเอกสาร
หมายเหตุ:บทความนี้กำหนดว่า คุณมีสำเนาของฐานข้อมูลตัวอย่าง Northwind.mdb จาก Microsoft Access 2000 จาก Microsoft Access 2002 หรือ จาก Microsoft Office Access 2003 ในการติดตั้ง
มี drawbacks เพื่อสร้างเอกสาร Word กับ ASP ฝั่งเซิร์ฟเวอร์ Automation:
- ประสิทธิภาพของ Microsoft Word จะทำงานอัตโนมัติออกกระบวนการเซิร์ฟเวอร์ การเรียกออกกระบวน take ในการดำเนินการ
- scalability เป็นข้อเซิร์ฟเวอร์ Automation, Microsoft Word มีค่าผลิต substantial เนื่องจากต้องโหลดแฟ้ม Winword.exe เพื่อให้คุณ reap ประโยชน์ของของ Word รุ่นที่ใช้วัตถุจำนวนมาก
โดยการสร้างเอกสารของคุณ จากการลบ และไม่ มีการใช้รูปแบบวัตถุ Word คุณสามารถทำให้โปรแกรมประยุกต์ ASP scaleable มากขึ้น และยัง เพิ่มประสิทธิภาพการทำงาน วิธีการนี้ถูกใช้ในตัวอย่างต่อไปนี้ที่สาธิตวิธีใช้นี้
FileSystemObjectเมื่อต้องการสร้างแฟ้มการ RTF ที่สามารถตีความใน Microsoft Word ที่ไคลเอ็นต์ของเบราว์เซอร์
ตัวอย่างแรกแสดงวิธีการใช้ ASP เพื่อสร้างเป็น RTF เอกสารที่ประกอบด้วยหัวกระดาษและท้ายกระดาษ ประกอบด้วยตาราง และใช้ลักษณะแบบอักษรที่แตกต่างกันและสีสำหรับข้อความทั่วทั้งเอกสาร ตัวอย่างแรกใช้ ADO ในการเข้าถึงข้อมูลจากฐานข้อมูล Northwind Access เพื่อสร้างเอกสาร 90-plus หน้าตัวอย่าง
ตัวอย่างที่สองแสดงวิธีการสร้างเอกสารเป็น RTF เหมือนกับที่ที่อยู่ของจดหมายเวียนของ Word เอกสารที่เป็นผลลัพธ์ประกอบด้วยหัวกระดาษและท้ายกระดาษ ต่าง ๆ ย่อหน้าตัว แบ่งการจัดรูปแบบและหน้า และลักษณะแบบอักษรที่แตกต่างกันใช้ และชนิดผ่านเอกสาร ตัวอย่างนี้ยังใช้ ADO ในการเข้าถึงข้อมูลจากฐานข้อมูล Northwind เพื่อสร้างเอกสาร 170-plus หน้า
หมายเหตุ:ข้อมูลจำเพาะเกี่ยวกับการจัดรูปแบบข้อความ Rich (RTF) คือ ข้อมูลจำเพาะสาธารณะในการสร้างแฟ้มข้อความเข้ากันได้กับ RTF คุณสามารถใช้เอกสารประกอบสำหรับข้อมูลจำเพาะเกี่ยวกับเว็บไซต์ต่อไปนี้ของ Microsoft สำหรับนักพัฒนาเครือข่าย (MSDN) เป็นทรัพยากรที่ช่วยให้คุณสร้างแฟ้มของคุณเอง RTF
อย่างไรก็ตาม ข้อมูลจำเพาะเกี่ยวกับการทำงานที่ให้ไว้ "ตาม-คือ", และไม่ใช่ supportted โดยฝ่ายสนับสนุนด้านเทคนิคของ Microsoft รุ่นล่าสุดของข้อมูลจำเพาะ RTF ดู MSDN เว็บไซต์ต่อไปนี้:
ตัวอย่าง asp 1
ASP ที่ต่อไปนี้อธิบายการสร้างเป็น RTF 90-plus หน้าเอกสารที่ประกอบด้วยการรายงานของตัวเลขที่คำนวณ
หมายเหตุ:ตัวแปร
sConnในรหัสนี้ ตัวอย่างประกอบด้วยเส้นทางไปยังฐานข้อมูล Northwind
ยืนยันว่า เส้นทางที่ระบุให้เหมาะสมสำหรับการติดตั้ง Office
คุณยังต้องการจัดหา
การเขียนเข้าถึงโฟลเดอร์ที่ประกอบด้วยการ ASP เพื่อให้คุณสามารถสร้างเอกสาร การเข้าถึงนี้ควรมีการระบุบุคคลภายใต้โดเมน Windows NT แต่สามารถนำไปใช้กับ
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 หรือฟอร์มจดหมาย แต่ละหน้าในเอกสาร 170-plus หน้าถูกสร้างขึ้นจากระเบียนในชุดระเบียน ADO
หมายเหตุ:ตัวแปร
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 Knowledge Base::
257757
(http://support.microsoft.com/kb/257757/
)
การพิจารณาเรื่องการทำงานอัตโนมัติของ Office ที่ด้านเซิร์ฟเวอร์
193998
(http://support.microsoft.com/kb/193998/
)
วิธีการอ่าน และแสดงข้อมูลไบนารีใน ASP
266263
(http://support.microsoft.com/kb/266263/
)
BUG: Word 2000 และ Excel 2000 แสดงแหล่งที่มา ASP เมื่อใช้ชนิดข้อมูลของกระแสข้อมูล MIME
247318
(http://support.microsoft.com/kb/247318/
)
BUG: Word 2000 และ Excel 2000 ไม่เปลี่ยนเส้นทางอย่างถูกต้องเมื่อใช้ Response.Redirect
หมายเลขบทความ (Article ID): 270906 - รีวิวครั้งสุดท้าย: 8 มกราคม 2554 - Revision: 2.0
ใช้กับ
- Microsoft Office Word 2007
- Microsoft Word 2000 Standard Edition
- Microsoft Word 97 Standard Edition
- Microsoft Active Server Pages 4.0
| kbexpertiseinter kbautomation kbfso kbhowto kbprogramming kbmt KB270906 KbMtth |
แปลโดยคอมพิวเตอร์ข้อมูลสำคัญ: บทความนี้แปลโดยซอฟต์แวร์การแปลด้วยคอมพิวเตอร์ของ Microsoft แทนที่จะเป็นนักแปลที่เป็นบุคคล Microsoft มีบทความที่แปลโดยนักแปลและบทความที่แปลด้วยคอมพิวเตอร์ เพื่อให้คุณสามารถเข้าถึงบทความทั้งหมดในฐานความรู้ของเรา ในภาษาของคุณเอง อย่างไรก็ตาม บทความที่แปลด้วยคอมพิวเตอร์นั้นอาจมีข้อบกพร่อง โดยอาจมีข้อผิดพลาดในคำศัพท์ รูปแบบการใช้ภาษาและไวยากรณ์ เช่นเดียวกับกรณีที่ชาวต่างชาติพูดผิดเมื่อพูดภาษาของคุณ Microsoft ไม่มีส่วนรับผิดชอบต่อความคลาดเคลื่อน ความผิดพลาดหรือความเสียหายที่เกิดจากการแปลเนื้อหาผิดพลาด หรือการใช้บทแปลของลูกค้า และ Microsoft มีการปรับปรุงซอฟต์แวร์การแปลด้วยคอมพิวเตอร์อยู่เป็นประจำ
ต่อไปนี้เป็นฉบับภาษาอังกฤษของบทความนี้:
270906
(http://support.microsoft.com/kb/270906/en-us/
)