利用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;
                }
            }


 

发布了38 篇原创文章 · 获赞 4 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/tomatozq/article/details/8546382