1. 程式人生 > >利用word的xml格式實現資料填充

利用word的xml格式實現資料填充

    以前利用word做模板都是用書籤來做佔位符,但缺點就是插進去的書籤顯示不明顯,無法在文件中直接顯示,現在發現用xml格式會方便很多。

    操作步驟如下:

   1.編寫例項類。

    public class Student {
        private int no;

        public int No
        {
            get { return no; }
            set { no = value; }
        }

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private int age;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }
    }


2.開啟vs的命令視窗,輸入xsd entity.dll,生成xsd檔案。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Student" nillable="true" type="Student" />
  <xs:complexType name="Student">
    <xs:sequence>
      <xs:element minOccurs="1" maxOccurs="1" name="No" type="xs:int" />
      <xs:element minOccurs="0" maxOccurs="1" name="Name" type="xs:string" />
      <xs:element minOccurs="1" maxOccurs="1" name="Age" type="xs:int" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>


3.新建word檔案,選擇模板及載入項xml架構新增生成的xsd檔案。

4.編輯word模板並將xsd中的節點拖放到文件中:

5.讀取模板填充節點另存為doc文件。

 object oMissing = System.Reflection.Missing.Value;
            Object oTrue = true;
            Object oFalse = false;

            Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document oWordDoc = new Microsoft.Office.Interop.Word.Document();

            object tmplPath = "E:\\專案\\WordXmlSchema\\學生簡歷.xml";

            try {
                oWordDoc = WordApp.Documents.Add(ref tmplPath, ref oMissing, ref oMissing, ref oMissing);

                Microsoft.Office.Interop.Word.XMLNode noNode = oWordDoc.SelectSingleNode("//ns0:No", "xmlns:ns0=\"http://www.jposft.com.cn\"");
                noNode.Text = "4210021983";

                Microsoft.Office.Interop.Word.XMLNode nameNode = oWordDoc.SelectSingleNode("//ns0:Name", "xmlns:ns0=\"http://www.jposft.com.cn\"");
                nameNode.Text = "鄭強";

                Microsoft.Office.Interop.Word.XMLNode ageNode = oWordDoc.SelectSingleNode("//ns0:Age", "xmlns:ns0=\"http://www.jposft.com.cn\"");
                ageNode.Text = "30";

                object oFilePath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\學生簡歷.doc";
                object oFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;

                oWordDoc.SaveAs(ref oFilePath, ref oFormat, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                MessageBox.Show("已匯出完畢!");
            }
            catch (System.Exception ex) {
                MessageBox.Show("匯出失敗:" + ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally {
                if (oWordDoc != null) {
                    oWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordDoc);
                    oWordDoc = null;
                }

                //關閉wordApp
                WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
                if (WordApp != null) {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp);
                    WordApp = null;
                }
            }