使用word的FormField预先插入占位符,然后在代码中获取所有FormField,进行替换。这种场景特别适用于,模版固定动态更改内容。看看最终效果

Spire.Doc组件读取与写入Word

    表单域制作步骤

    1.打开word中的开发工具选项,对于导航栏中没有这一项的,可以通过 文件->选项->自定义功能区->开发工具 进行打开

    Spire.Doc组件读取与写入Word

   2.插入TextField, 属性->设置书签名称

   Spire.Doc组件读取与写入Word

 Spire.Doc组件读取与写入Word

  模版制作完成后,来看看实现代码

 

Spire.Doc组件读取与写入Word
class Program
    {
        private static BindingFlags s_flag = BindingFlags.Instance | BindingFlags.Public;

        static void Main(string[] args)
        {
            Console.WriteLine("\nRunning Examples");

            Resume personResume = new Resume
            {

                Name = "Spire.Doc",//姓名
                Marriage = "未婚",//婚姻
                Birth = "2010-09-19",//出生年月
                Political = "团员",//政治面貌
                Sex = "男",//性别
                Nation = "汉",//民族
                Degree = "本科",//学位
                Mobile = "13567890987",//移动电话
                Professional = "软件",//专业
                Email = "2345678@qq.com",//邮箱
                Adress = "", //地址
                MajorCourse = "数据结构,C语言,算法,C++",//主修课程
                PersonalAbility = "熟练掌握DocX操作Word,SQL能力强悍",//个人能力
                ComputerAbility = "高级软件工程师",//计算机能力
                LanguageLevel = "CET-4,CET-6",//外语水平
                Awards = "网络技术协会负责人,……………………",//奖励情况
                SelfEvaluation = "本人性格开朗、纪律性强,工作积极配合;意志坚强,具有较强的无私奉献精神"//自我评价
            };
            CreateResume(personResume);

            Console.WriteLine("\nPress any key to exit.");
            Console.ReadKey();
        }

        private static void CreateResume(Resume personResume)
        {
            Document doc = new Document(@"ResumeTemplate.docx");
            //清除表单域阴影
            doc.Properties.FormFieldShading=false;
            try
            {
                Type type = typeof(Resume);
                PropertyInfo[] properties = type.GetProperties(s_flag);
                int length = properties.Length;
                Dictionary<string, PropertyInfo> dict = new Dictionary<string, PropertyInfo>(length, StringComparer.OrdinalIgnoreCase);
                foreach (PropertyInfo prop in properties)
                {
                    dict[prop.Name] = prop;
                }
                object value = null;
                foreach (FormField field in doc.Sections[0].Body.FormFields)
                {
            //field.name对应设置模版文本域名称 PropertyInfo prop = dict[field.Name]; if (prop != null) { value = prop.GetValue(personResume, null); if (value != null && value != DBNull.Value) { switch (field.Type) { case FieldType.FieldFormTextInput: field.Text = value.ToString(); break; default: break; } } } } doc.SaveToFile(@"DocXResume.docx", FileFormat.Docx); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
Spire.Doc组件读取与写入Word

 

Spire.Doc组件读取与写入Word
Spire.Doc组件读取与写入Word
  public class Resume
    {
        public string Name { get; set; }

        public string Marriage { get; set; }

        public string Birth { get; set; }

        public string Political { get; set; }

        public string Sex { get; set; }

        public string Nation { get; set; }

        public string Degree { get; set; }

        public string Mobile { get; set; }

        public string Professional { get; set; }

        public string Email { get; set; }

        public string Adress { get; set; }

        public string MajorCourse { get; set; }

        public string PersonalAbility { get; set; }

        public string ComputerAbility { get; set; }

        public string LanguageLevel { get; set; }

        public string Awards { get; set; }

        public string SelfEvaluation { get; set; }
    }
Spire.Doc组件读取与写入Word

  重点关注上面标红的代码,可以看到通过简单的代码即可完成一份简历文档 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-19
  • 2022-12-23
  • 2021-04-02
  • 2022-12-23
  • 2023-03-20
猜你喜欢
  • 2021-07-24
  • 2021-12-25
  • 2022-01-15
  • 2022-01-16
  • 2021-07-15
  • 2021-09-03
  • 2021-08-31
相关资源
相似解决方案