利用word的xml格式实现数据填充

本文介绍了如何利用XML格式来替代传统书签作为Word模板的占位符,通过编写实例类,生成xsd文件,然后在Word中添加XML架构并编辑模板,实现更直观的数据填充,最终读取模板并生成doc文档。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值