Thứ Hai, 27 tháng 7, 2009

Export InfoPath form to Word 2003 document sử dụng XSLT và XPath

Khi làm việc với InfoPath, nhiều khi chúng ta có nhu cầu Export ra file .doc để người dùng không phải IT pro có thể chỉnh sửa theo ý mình. Bài viết Convert an InfoPath form into a Word 2003 document and send this as an attachment in an e-mail trình bày cho các bạn cách để thực hiện điều đó. Tuy nhiên, bài viết này mới chỉ dừng lại ở việc Export giữ liệu tĩnh, nghĩa là khi chúng ta thay đổi nội dung trên form, thì khi export ra file .doc lại không thay đổi.

Bài viết này trình bày cho các bạn một cách đầy đủ, cách để Export InfoPath form ra Word 2003 (chú ý, là Word 2003. Để export ra Word 2007 tôi sẽ trình bày trong một bài viết khác).

1. Thiết kế một InfoPath form như sau:

A21319952F68E79E_482_0[1]

2. Fill form này và lưu thành file Form1.xml

A21319952F68E79E_482_1[1]

3. Mở file bạn vừa save từ InfoPath form (Form1.xml) trên Word. Sau đó tiến hành thêm nội dung, chỉnh sửa giao diện của file xml này trên Word.

Sau khi format xong, chúng ta chọn File –> Save As. Trong hộp thoại Save As, chú ý là chúng ta bỏ chọn Save data only checkbox và đặt tên file là Form1_1.xml

A21319952F68E79E_482_2[1] A21319952F68E79E_482_3[1]

4. Việc tiếp theo là chúng ta download và cài đặt WordML Transform Inference Tool. Sau đó chúng ta sử dụng cmd để thực thi việc tạo XSLT file như sau:

WML2XSLT.EXE "C:\InfoPath\ExportToWord2003\Form1_1.xml" -o "C:\InfoPath\ExportToWord2003\XMLToWordML.xsl"





Chúng ta bấm Select All và click Ok nếu hộp thoại “Select namespaces” xuất hiện.


A21319952F68E79E_482_4[1] 


5. Quay trở lại InfoPath trong trạng thái Design mode. Chúng ta chọn File –> Save as source files


A21319952F68E79E_482_5[1]


6. Mở myschema.xsd trong SourceFile folder vừa export từ InfoPath, và XMLToWordML.xsl vừa mới tạo từ tool, bằng một Text editor nào đó. Sau đó chúng ta copy 2 namespace từ xsd sang xsl như sau:


myschema.xsd

 


<xsd:schema targetNamespace="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-07-27T07:17:59" 
...
xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-07-27T07:17:59" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
...
>


XMLToWordML.xsl sau khi copy



<xsl:stylesheet version="1.0" 
xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-07-27T07:17:59" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
...
>

7. Tiếp theo chúng ta tìm đến các nội dung: “Quang” và “Nguyen Ba” được bao trong thẻ <xsl:text>….</xsl:text> của xsl, để thay thế nó bằng XPath như sau.


<xsl:text>Quang</xsl:text> –> <xsl:value-of select="my:myFields/my:firstName" />


<xsl:text>Nguyen Ba</xsl:text> –> <xsl:value-of select="my:myFields/my:lastName" />


<xsl:text>http://basquang.spaces.live.com</xsl:text> –> <xsl:value-of select="my:myFields/my:website" />


Sau đó chúng ta save file này.


8. Quay trở lại InfoPath form ở dạng Design mode. Chúng ta chọn Tools –> Data Connections… và chọn Add button. Trong hộp thoại Data Connection Wizard chúng ta chọn “Receive Data” và trỏ tới file XMLToWordML.xsl và để mặc định các lựa chọn của Wizard. Chú ý tên cho data connection là XMLToWordML


A21319952F68E79E_482_6[1]


9. Vào Tools –> Options để kiểm tra Default programming language khi design form bằng InfoPath là được thiết lập cho JScript


A21319952F68E79E_482_7[1]


10.  Click đúp vào “Export to Word 2003” button và chọn Edit Form Code. Sau đó Add đoạn code sau trong OnClick event và Save InfoPath.


A21319952F68E79E_482_8[1] A21319952F68E79E_482_9[1]



var formInXML = XDocument.DOM;
var xslXMLToWordML = XDocument.GetDOM("XMLToWordML"); 
var formInWordML = formInXML.transformNode(xslXMLToWordML);
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(formInWordML);
xmlDoc.save("C:\\InfoPath\\InfoPathFormInWordML.xml");
var wordApp = new ActiveXObject("Word.Application");
wordApp.Documents.Open("C:\\InfoPath\\InfoPathFormInWordML.xml");
wordApp.Visible = true;

 


11. Mở Tools –> Form Options –> Security and Trust  để chọn Full Trust và Sign this form


A21319952F68E79E_482_10[1]







12. Giờ chúng ta sẵn sàng sử dụng Export InfoPath to Word 2003 function


A21319952F68E79E_482_11[1]

Không có nhận xét nào:

Đăng nhận xét