Αυτό το άρθρο περιγράφει πώς να δημιουργήσει αρχεία μορφής εμπλουτισμένου κειμένου (RTF) με τη δέσμη ενεργειών ASP και στη συνέχεια να μεταδώσει αυτών των αρχείων στο Microsoft Word. Η τεχνική αυτή παρέχει μια εναλλακτική λύση διακομιστή αυτοματισμού του Microsoft Word για τη δημιουργία εγγράφων κατά το χρόνο εκτέλεσης.
ΣΗΜΕΙΩΣΗΑυτό το άρθρο απαιτεί να έχετε ένα αντίγραφο της βάσης δεδομένων Northwind.mdb δείγμα εγκατασταθεί από τη Microsoft Access 2000, η Microsoft Access 2002 ή Microsoft Office Access 2003.
Υπάρχουν μειονεκτήματα για τη δημιουργία εγγράφων του Word με τις σελίδες ASP διακομιστή αυτοματοποίησης:
- Επιδόσεις του Microsoft Word είναι ένα αυτοματισμού εκτός διεργασίας διακομιστή και λήψη κλήσεων εκτός διεργασίας περισσότερο χρόνο για να εκτελεστεί.
- Κλιμάκωση ως ένα αυτοματισμού του διακομιστή, το Microsoft Word έχει σημαντική επιβάρυνση, επειδή πρέπει να φορτωθεί το αρχείο Winword.exe προκειμένου να δρέψουν τα οφέλη του μοντέλου αντικειμένου εμπλουτισμένου του Word.
Κατά τη δημιουργία των εγγράφων σας, από την αρχή και χωρίς τη χρήση του μοντέλου αντικειμένου του Word, μπορείτε να κάνετε πιο scaleable εφαρμογή σας ASP και επίσης αύξηση των επιδόσεων. Η προσέγγιση αυτή χρησιμοποιείται στα ακόλουθα δείγματα που παρουσιάζουν τον τρόπο χρήσης του
FileSystemObjectΓια να δημιουργήσετε ένα αρχείο RTF που μπορεί να ερμηνευτεί στο Microsoft Word σε προγράμματα-πελάτες περιήγησης.
Το πρώτο δείγμα δείχνει πώς να χρησιμοποιήσετε τις ASP, για να δημιουργήσετε ένα έγγραφο RTF που περιέχει κεφαλίδες και υποσέλιδα, να περιέχει έναν πίνακα και να χρησιμοποιεί διαφορετικά στυλ και χρώματα για κείμενο σε ολόκληρο το έγγραφο. Το πρώτο δείγμα χρησιμοποιεί το ADO για να προσπελάσει δεδομένα από το δείγμα βάσης δεδομένων Northwind Access για να δημιουργήσετε ένα έγγραφο 90-plus-σελίδα.
Το δεύτερο δείγμα δείχνει πώς μπορείτε να δημιουργήσετε ένα έγγραφο RTF παρόμοια με αυτή μιας συγχώνευσης αλληλογραφίας του Word. Το έγγραφο που προκύπτει περιέχει κεφαλίδες και υποσέλιδα, διάφορα παράγραφο, οι αλλαγές σελίδας και μορφοποίηση, και χρησιμοποιεί διαφορετικά στυλ και τους τύπους μέσα στο έγγραφο. Αυτό το δείγμα χρησιμοποιεί επίσης το ADO για να προσπελάσει δεδομένα από τη βάση δεδομένων Northwind, για να δημιουργήσετε ένα έγγραφο 170-plus-σελίδα.
ΣΗΜΕΙΩΣΗΟι προδιαγραφές μορφή εμπλουτισμένου κειμένου (RTF) είναι μια δημόσια προδιαγραφή για τη δημιουργία αρχείων κειμένου RTF που είναι συμβατές. Μπορείτε να χρησιμοποιήσετε την τεκμηρίωση για την προδιαγραφή από την ακόλουθη τοποθεσία της Microsoft Developer Network (MSDN) στο Web ως πόρος για να σας βοηθήσει να δημιουργήσετε τα δικά σας αρχεία RTF.
Ωστόσο, παρέχεται η προδιαγραφή "ως-είναι", και δεν είναι supportted από την τεχνική υποστήριξη της Microsoft. Για την πιο πρόσφατη έκδοση της προδιαγραφής RTF, ανατρέξτε στην ακόλουθη τοποθεσία του MSDN στο Web:
Δείγματα ASP 1
Το παρακάτω ASP παρουσιάζει μια μορφή RTF 90-plus-σελίδα δημιουργίας εγγράφου που περιέχει μια αναφορά με τα υπολογιζόμενα ποσά.
ΣΗΜΕΙΩΣΗΗ μεταβλητή
sConnσε αυτόν τον κωδικό δείγμα περιέχει τη διαδρομή προς τη βάση δεδομένων Northwind.
Βεβαιωθείτε ότι η διαδρομή που δόθηκε είναι κατάλληλη για την εγκατάσταση του Office.
Πρέπει επίσης να δώσετε
Εγγραφή (Write)η πρόσβαση στο φάκελο που περιέχει τις ASP, ώστε να μπορείτε να δημιουργήσετε το έγγραφο. Αυτή η access πρέπει να περιορίζονται σε συγκεκριμένα άτομα, με έναν τομέα των 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
The following ASP demonstrates a simulation of a Word mail merge
or form letter. Each page in the 170-plus-page document is generated from a
record in an ADO recordset.
ΣΗΜΕΙΩΣΗThe variable
sConnin this code sample contains the path to the Northwind database.
Confirm that the path supplied is appropriate for your installation of Office.
You also need to supply
Εγγραφή (Write)access to the folder containing the ASP so the document can be
created. This access should be limited to specific individuals under an NT
domain, but can be applied to
Everyonefor anonymous access.
<%@ 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 & """>"
%>
Για περισσότερες πληροφορίες, κάντε κλικ στους αριθμούς των άρθρων παρακάτω για να προβάλετε τα άρθρα της Γνωσιακής Βάσης (Knowledge Base) της Microsoft:
257757
(http://support.microsoft.com/kb/257757/
)
Considerations for server-side automation of Office
193998
(http://support.microsoft.com/kb/193998/
)
How to read and display binary data in 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 and Excel 2000 do not redirect correctly when using Response.Redirect