Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.Text = "To File"
Button2.Text = "To Clipboard"
End Sub
Private Sub ButtonsClick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click, Button2.Click
Dim sPath As String = Directory.GetParent(Directory.GetCurrentDirectory()).ToString
' Open the XML file.
Dim xmlDoc As New XmlDocument()
xmlDoc.Load(sPath & "\Dictionary.xml")
' Open the XSL file.
Dim xslDoc As New XslTransform()
xslDoc.Load(sPath & "\Dictionary.xslt")
Select Case sender.name
Case "Button1"
' Transform the XSL and save it to file.
Dim TWrtr As New XmlTextWriter(sPath & "\Dictionary.RTF", System.Text.Encoding.Default)
xslDoc.Transform(xmlDoc, Nothing, TWrtr, Nothing)
TWrtr.Close()
MsgBox("Transformed RTF saved to " & sPath & "\Dictionary.RTF")
Case "Button2"
' Transform the XSL and copy it to the clipboard.
Dim SWrtr As New StringWriter()
xslDoc.Transform(xmlDoc, Nothing, SWrtr, Nothing)
Dim datObj As New DataObject(DataFormats.Rtf, SWrtr)
Clipboard.SetDataObject(datObj)
SWrtr.Close()
MsgBox("Transformed RTF copied to the clipboard.")
End Select
End Sub
เพิ่มแฟ้ม XML ในโครงการของคุณ:
ในการProjectเมนู คลิกเพิ่มรายการใหม่.
จากรายการของต้นแบบ คลิกแฟ้ม xml.
พิมพ์ชื่อDictionary.xmlแล้ว คลิกOPEN.
ผนวกต่อไปนี้เพื่อที่เนื้อหาของ Dictionary.xml:
<Dictionary>
<Entries>
<Entry>
<Word Type="1">Energetic</Word>
<Definition>Having, exerting, or displaying energy</Definition>
</Entry>
<Entry>
<Word Type="1">Happy</Word>
<Definition>Enjoying, displaying, or characterized by pleasure or joy</Definition>
</Entry>
<Entry>
<Word Type="2">Emotion</Word>
<Definition>A complex, strong subjective response</Definition>
</Entry>
</Entries>
</Dictionary>
คลิกไปยังแฟ้มเมื่อต้องการบันทึก transformed XML ไปยังแฟ้ม (Dictionary.rtf) คุณสามารถเปิดไฟล์ RTF ใน Word เพื่อตรวจสอบผลลัพธ์ของการแปลง
คลิกไปยังคลิปบอร์ดเมื่อต้องการคัดลอก transformed XML ไปยังคลิปบอร์ดของ Windows คุณสามารถวางเนื้อหาของคลิปบอร์ดแล้วลงในเอกสาร Word ใหม่ หรือที่มีอยู่เพื่อดูผลลัพธ์
หมายเหตุ:: รหัสต่อไปนี้การสันนิษฐานคุณได้ติดตั้งอยู่บน localhost ใน SQL Server ถ้าคุณต้องใช้คอมพิวเตอร์เครื่องอื่น เปลี่ยนสมาชิกของแหล่งข้อมูลของสายอักขระการเชื่อมต่ออย่างเหมาะสม
Private Sub ButtonsClick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click, Button2.Click
' Connect to the data source.
Dim nwindConn As SqlConnection = New SqlConnection( _
"Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI")
nwindConn.Open()
' Build a dataset based on whether you requested to view a list of
' orders or a list of contacts.
Dim ds As DataSet
Dim sXSL As String
Select Case (sender.id)
Case "Button1"
ds = New DataSet("Contacts")
Dim ContactsDA As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Customers", nwindConn)
ContactsDA.Fill(ds, "Customers")
' XSLT to use for transforming this dataset.
sXSL = "Contacts.xslt"
Case "Button2"
ds = New DataSet("CustomerOrders")
Dim custDA As SqlDataAdapter = New SqlDataAdapter("SELECT CustomerID, CompanyName, " & _
"Address, City, Region, PostalCode, Country FROM Customers", nwindConn)
custDA.Fill(ds, "Customers")
Dim ordersDA As SqlDataAdapter = New SqlDataAdapter("SELECT OrderID, CustomerID, Freight " & _
"FROM Orders", nwindConn)
ordersDA.Fill(ds, "Orders")
Dim ordersdetailDA As SqlDataAdapter = New SqlDataAdapter( _
"SELECT [Order Details].OrderID, Products.ProductName, [Order Details].Quantity, " & _
"[Order Details].[UnitPrice]*[Quantity]*(1-[Discount]) AS ItemTotal " & _
"FROM Products INNER JOIN [Order Details] ON Products.ProductID = [Order Details].ProductID " _
, nwindConn)
ordersdetailDA.Fill(ds, "OrderDetails")
nwindConn.Close()
ds.Relations.Add("CustOrders", _
ds.Tables("Customers").Columns("CustomerID"), _
ds.Tables("Orders").Columns("CustomerID")).Nested = True
ds.Relations.Add("OrdersToOrdersDetail", _
ds.Tables("Orders").Columns("OrderID"), _
ds.Tables("OrderDetails").Columns("OrderID")).Nested = True
' XSLT to use for transforming this dataset.
sXSL = "CustOrders.xslt"
End Select
' Close the connection to the data source.
nwindConn.Close()
' Transform the dataset by using the appropriate stylesheet.
Dim xmlDoc As XmlDataDocument = New XmlDataDocument(ds)
Dim xslTran As XslTransform = New XslTransform()
xslTran.Load(Server.MapPath(sXSL))
' Stream the results of the transformation to Word.
Response.ContentType = "application/msword"
Response.Charset = ""
Response.ContentEncoding = System.Text.Encoding.Default
xslTran.Transform(xmlDoc, Nothing, Response.Output)
End Sub
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-scripts" >
<xsl:output method="text"/>
<msxsl:script language="VB" implements-prefix="user">
Dim CustomerTotal as Double = 0
Dim OrderSubtotal as Double = 0
Function AddToOrderSubtotal(amt)
amt.MoveNext
OrderSubtotal = OrderSubtotal + System.Convert.ToDouble(amt.Current.Value)
End Function
Function GetOrderSubtotal
GetOrderSubtotal = OrderSubtotal
End Function
Function GetCustomerTotal
GetCustomerTotal = CustomerTotal
CustomerTotal = 0
End Function
Function GetOrderTotal(freight)
freight.MoveNext
nFreight = System.Convert.ToDouble(freight.Current.Value)
GetOrderTotal = nFreight + OrderSubtotal
CustomerTotal = nFreight + OrderSubtotal + CustomerTotal
OrderSubtotal = 0
End Function
</msxsl:script>
<xsl:template match="CustomerOrders">
<xsl:text>{\rtf1</xsl:text>
<xsl:text>{\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;\red221\green221\blue221;}</xsl:text>
<xsl:text>{\info{\title Sample RTF Document}{\author Microsoft Developer Support}}</xsl:text>
<xsl:text>{\header\pard\qc{\fs50 ASP-Generated RTF\par}{\fs18\chdate\par}\par\par}</xsl:text>
<xsl:text>{\footer\pard\qc\brdrt\brdrs\brdrw10\brsp100\fs18 Page {\field{\*\fldinst PAGE}</xsl:text>
<xsl:text>{\fldrslt }} of {\field{\*\fldinst NUMPAGES}{\fldrslt 1}} \par}</xsl:text>
<xsl:apply-templates select="Customers"/>
<xsl:text>}</xsl:text>
</xsl:template>
<xsl:template match="Customers">
<xsl:text>\par\pard\fs20\cf2\qr\b </xsl:text><xsl:value-of select="CustomerID"/><xsl:text>\cf0\b0</xsl:text>
<xsl:text>\par\pard </xsl:text><xsl:value-of select="CompanyName"/>
<xsl:text>\par </xsl:text><xsl:value-of select="Address"/>
<xsl:text>\par </xsl:text><xsl:value-of select="City"/>
<xsl:text>, </xsl:text><xsl:value-of select="Region"/>
<xsl:text> </xsl:text><xsl:value-of select="PostalCode"/>
<xsl:text>\par </xsl:text><xsl:value-of select="Country"/>
<xsl:text>\par\par</xsl:text>
<xsl:apply-templates select="Orders"/>
<xsl:text>\trowd\cellx7000\cellx9000\pard\intbl\ql\b\cbpat1 </xsl:text>
<xsl:text>Order Total for the Current Period:\cell </xsl:text>
<xsl:text>\qr</xsl:text>
<xsl:variable name="CustTtl" select="user:GetCustomerTotal()"/>
<xsl:value-of select="format-number($CustTtl,'$###0.00')"/>
<xsl:text>\cell</xsl:text>
<xsl:text>\pard\intbl\row</xsl:text>
<xsl:text>\pard\par\pard</xsl:text>
<xsl:text>\pard\plain\fs18\cf6\qc</xsl:text>
<xsl:choose>
<xsl:when test="$CustTtl = 0">
<xsl:text>\b We've missed hearing from you!\b0 </xsl:text>
<xsl:text> At your convenience, please call your personal sales representative </xsl:text>
<xsl:text>so that we may discuss our specials for new and returning customers!</xsl:text>
</xsl:when>
<xsl:when test="$CustTtl > 2000">
<xsl:text>\b Congratulations!\b0 Your purchases for this period qualify you for a \b 20%\b0 </xsl:text>
<xsl:text> discount on one of your next orders. To take advantage of this offer, provide </xsl:text>
<xsl:text>the coupon code ABC123XYZ when placing your order.</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text> We value your patronage with Northwind Traders and would love to hear from you. </xsl:text>
<xsl:text>If you have any questions about our upcoming line of products or if you want </xsl:text>
<xsl:text>a catalog for the coming season, call 1-888-000-000.</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:text>\par\pard</xsl:text>
<xsl:text>\par \page</xsl:text>
</xsl:template>
<xsl:template match="Orders">
<xsl:text>\trowd\cellx9000\pard\intbl\cbpat9</xsl:text>
<xsl:text>\ql\b </xsl:text><xsl:value-of select="OrderID"/><xsl:text>\b0\cell </xsl:text>
<xsl:text>\pard\intbl\row</xsl:text>
<xsl:apply-templates select="OrderDetails"/>
<xsl:text>\trowd\cellx7000\cellx9000\pard\intbl</xsl:text>
<xsl:text>\qr Subtotal:\cell </xsl:text>
<xsl:value-of select="format-number(user:GetOrderSubtotal(),'$###0.00')"/><xsl:text>\cell</xsl:text>
<xsl:text>\pard\intbl\row</xsl:text>
<xsl:text>\trowd\cellx7000\cellx9000\pard\intbl</xsl:text>
<xsl:text>\qr Freight:\cell </xsl:text>
<xsl:value-of select="format-number(Freight,'$###0.00')"/><xsl:text>\cell</xsl:text>
<xsl:text>\pard\intbl\row</xsl:text>
<xsl:text>\trowd\cellx7000\cellx9000\pard\intbl</xsl:text>
<xsl:text>\qr Total:\cell </xsl:text>
<xsl:value-of select="format-number(user:GetOrderTotal(Freight), '$###0.00')"/><xsl:text>\cell</xsl:text>
<xsl:text>\pard\intbl\row</xsl:text>
<xsl:text>\trowd\cellx9000\pard\intbl \cell\pard\intbl\row</xsl:text>
</xsl:template>
<xsl:template match="OrderDetails">
<xsl:text>\trowd\cellx5000\cellx7000\cellx9000\pard\intbl\ql </xsl:text>
<xsl:value-of select="ProductName"/><xsl:text>\cell </xsl:text>
<xsl:text>\qc </xsl:text><xsl:value-of select="Quantity"/><xsl:text>\cell </xsl:text>
<xsl:text>\qr </xsl:text>
<xsl:value-of select="format-number(ItemTotal,'$###0.00')"/><xsl:text>\cell</xsl:text>
<xsl:variable name="RunTotal" select="user:AddToOrderSubtotal(ItemTotal)"/>
<xsl:text>\pard\intbl\row</xsl:text>
</xsl:template>
</xsl:stylesheet>
ในการการสร้างเมนู คลิกสร้างโซลูชัน.
เริ่มการทำงานของ Internet Explorer และเรียกดู http://localhost/RTFDemo/Webform1.aspx
คลิกดูข้อมูลที่ติดต่อเมื่อต้องการดูการแปลง XML แรกเพื่อ RTF ใน Word
คลิกสำรองข้อมูลใน Internet Explorer
คลิกใบสั่งของลูกค้ามุมมองเมื่อต้องการดูการแปลง XML ที่สองเพื่อ RTF ใน Word
Dim writer As XmlTextWriter = New XmlTextWriter( _
Server.MapPath("Results.doc"), System.Text.Encoding.Default)
xslTran.Transform(xmlDoc, Nothing, writer)
writer.Close()
Response.Redirect("Results.doc")
การจัดเก็บ RTF ไปยังแฟ้มในลักษณะนี้ให้คุณตรวจสอบโครงสร้างของ RTF ในแฟ้ม โดยใช้ข้อความได้อย่างง่ายดายแก้ไข เช่น Notepad
Storing the RTF to a file can be a helpful troubleshooting technique if the XSL
transformation does not produce the results you expect.
When transforming to RTF, be aware of how you present
whitespace and carriage returns in your stylesheet because that can affect how
Word interprets your RTF. Both code samples in this article use the<xsl:text></xsl:text>element because it forces any white space information in it to be
retained.
การใช้<xsl:output method="text"></xsl:output>in your stylesheet to make sure that your XML is transformed to
text (rather than XML, which is the default output method). If you do not
specify text as the output method, XML processing instructions may be added to
the file. This can prevent Word from correctly interpeting the text as
RTF.
For additional information about
server-side Automation of Microsoft Word and other Office applications, click
the article number below to view the article in the Microsoft Knowledge Base:
ข้อมูลสำคัญ: บทความนี้แปลโดยซอฟต์แวร์การแปลด้วยคอมพิวเตอร์ของ Microsoft แทนที่จะเป็นนักแปลที่เป็นบุคคล Microsoft มีบทความที่แปลโดยนักแปลและบทความที่แปลด้วยคอมพิวเตอร์ เพื่อให้คุณสามารถเข้าถึงบทความทั้งหมดในฐานความรู้ของเรา ในภาษาของคุณเอง อย่างไรก็ตาม บทความที่แปลด้วยคอมพิวเตอร์นั้นอาจมีข้อบกพร่อง โดยอาจมีข้อผิดพลาดในคำศัพท์ รูปแบบการใช้ภาษาและไวยากรณ์ เช่นเดียวกับกรณีที่ชาวต่างชาติพูดผิดเมื่อพูดภาษาของคุณ Microsoft ไม่มีส่วนรับผิดชอบต่อความคลาดเคลื่อน ความผิดพลาดหรือความเสียหายที่เกิดจากการแปลเนื้อหาผิดพลาด หรือการใช้บทแปลของลูกค้า และ Microsoft มีการปรับปรุงซอฟต์แวร์การแปลด้วยคอมพิวเตอร์อยู่เป็นประจำ