之前写了一篇开源组件DocX读写word的文章,当时时间比较匆忙选了这个组件,使用过程中还是有些不便,不能提前定义好模版,插入Form表单域进行替换。最近无意中发现Spire.Doc组件功能很强大,目前来看基本上符合我的所有使用场景。本篇将挑选几个重要的应用场景进行介绍。

阅读目录

使用模版生成简历

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

表单域制作步骤

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

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

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

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 = "",//移动电话
Professional = "软件工程",//专业
Email = "2345678@qq.com",//邮箱
Adress = "故宫", //地址
MajorCourse = "数据结构,C语言,算法,C++",//主修课程
PersonalAbility = "熟练掌握DocX操作Word,SQL能力强悍",//个人能力
ComputerAbility = "高级软件工程师",//计算机能力
LanguageLevel = "CET-4,CET-6",//外语水平
Awards = "1999年几月 曾获优秀班干部,3等奖学金1999年几月 曾获校优秀干部,学生会先进集体,2等奖学金20**年几月 曾获优秀学习委员,网络技术协会负责人,……………………",//奖励情况
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[].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);
}
}
}
  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转换成HTML,RTF,PDF,TXT,WPS...等格式

Document doc = new Document(@"SpireDocResume.docx"); //Save doc file.
doc.SaveToFile("SpireDocResume.html", FileFormat.Html);
    效果和直接打开word是一样的,有了这功能就能实现在线word预览,之前的一篇在线文档预览方案也可以参考一下。其它格式的转换也是一样的代码,改一下FileFormat枚举值即可。

Table操作

 private static void addTable(Section section)
{
String[] header = { "Name", "Capital", "Continent", "Area", "Population" };
String[][] data =
{
new String[]{"Argentina", "Buenos Aires", "South America", "", ""},
new String[]{"Bolivia", "La Paz", "South America", "", ""},
new String[]{"Brazil", "Brasilia", "South America", "", ""},
new String[]{"Canada", "Ottawa", "North America", "", ""},
new String[]{"Chile", "Santiago", "South America", "", ""},
new String[]{"Colombia", "Bagota", "South America", "", ""},
new String[]{"Cuba", "Havana", "North America", "", ""},
new String[]{"Ecuador", "Quito", "South America", "", ""},
new String[]{"El Salvador", "San Salvador", "North America", "", ""},
new String[]{"Guyana", "Georgetown", "South America", "", ""},
new String[]{"Jamaica", "Kingston", "North America", "", ""},
new String[]{"Mexico", "Mexico City", "North America", "", ""},
new String[]{"Nicaragua", "Managua", "North America", "", ""},
new String[]{"Paraguay", "Asuncion", "South America", "", ""},
new String[]{"Peru", "Lima", "South America", "", ""},
new String[]{"United States of America", "Washington", "North America", "", ""},
new String[]{"Uruguay", "Montevideo", "South America", "", ""},
new String[]{"Venezuela", "Caracas", "South America", "", ""}
};
Spire.Doc.Table table = section.AddTable();
table.ResetCells(data.Length + , header.Length); // ***************** First Row *************************
TableRow row = table.Rows[];
row.IsHeader = true;
row.Height = ; //unit: point, 1point = 0.3528 mm
row.HeightType = TableRowHeightType.Exactly;
row.RowFormat.BackColor = Color.Gray;
for (int i = ; i < header.Length; i++)
{
row.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
Paragraph p = row.Cells[i].AddParagraph();
p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
TextRange txtRange = p.AppendText(header[i]);
txtRange.CharacterFormat.Bold = true;
} for (int r = ; r < data.Length; r++)
{
TableRow dataRow = table.Rows[r + ];
dataRow.Height = ;
dataRow.HeightType = TableRowHeightType.Exactly;
dataRow.RowFormat.BackColor = Color.Empty;
for (int c = ; c < data[r].Length; c++)
{
dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
dataRow.Cells[c].AddParagraph().AppendText(data[r][c]);
}
}
}

总结

通过上面三个简单的例子,粗略的了解了Spire.Doc。下面就我个人对DocX和Spire.Doc使用,列出两种优缺点。

  Spire.Doc DocX
API 介绍简单 无API介绍
Demo 提供了很多Demo方便学习 demo少
收费 收费 开源免费
功能对比 1.支持FormField模版替换 2.Table读写功能强大 1.对自定义属性读写存在BUG
兼容性 兼容word各版本 支持2007即以上版本
依赖性 不依赖于office,即使服务器未装office也能正常操作

实际开发中可以根据自己需要来选择使用Sprie.Doc或者DocX。

本文例子Demo下载地址:SpireDoc_Demo

如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的推荐按钮。
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的关注我

如果,想给予我更多的鼓励,求打

因为,我的写作热情也离不开您的肯定支持。

感谢您的阅读,如果您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客,我是焰尾迭 。

Spire.Doc组件读取与写入Word的更多相关文章

  1. 使用Spire.Doc组件利用模板导出Word文档

    以前一直是用Office的组件实现Word文档导出,但是让客户在服务器安装Office,涉及到版权:而且Office安装,包括权限配置也是比较麻烦. 现在流行使用第三方组件来实现对Office的操作, ...

  2. 数据字典生成工具之旅(5):DocX组件读取与写入Word

    由于上周工作比较繁忙,所以这篇文章等了这么久才写(预告一下,下一个章节正式进入NVelocity篇,到时会讲解怎么使用NVelocity做一款简易的代码生成器,敬请期待!),好了正式进入本篇内容. 这 ...

  3. DocX组件读取与写入Word

    本文转载:http://www.cnblogs.com/yanweidie/p/3861482.html 由于上周工作比较繁忙,所以这篇文章等了这么久才写(预告一下,下一个章节正式进入NVelocit ...

  4. word模板导出的几种方式:第三种:标签替换(DocX组件读取与写入Word)

    dll文件下载地址:https://files-cdn.cnblogs.com/files/daizhipeng/DocX.rar DocX wordDocumentOld = DocX.Load(S ...

  5. Spire.Doc组件

    使用Spire.Doc组件利用模板导出Word文档 以前一直是用Office的组件实现Word文档导出,但是让客户在服务器安装Office,涉及到版权:而且Office安装,包括权限配置也是比较麻烦. ...

  6. [php]在PHP中读取和写入WORD文档的代码

    测试平台windows 使用的windows的com主键. <? // 建立一个指向新COM组件的索引 $word = new COM("word.application") ...

  7. 【好文翻译】一步一步教你使用Spire.Doc转换Word文档格式

    背景: 年11月,微软宣布作为ECMA国际主要合作伙伴,将其开发的基于XML的文件格式标准化,称之为"Office Open XML" .Open XML的引进使office文档结 ...

  8. Spire.Doc 生成pdf业务运营报告

    需求:每天向全国各运营大区钉钉运营群定时发送pdf业务运营报告: 通过对各Office操作组件对比,选择Spire.Doc.它专门为开发人员进行创建,读取,写入.转换打印 word 文档文件提供便利, ...

  9. 使用Spire.Doc来转换文本

    使用Spire.Doc来转换文本 前段时间,我为不熟悉这个产品的读者们写了一篇关于我对 Spire.Doc的初识印象.Spire.Doc是一个专业的Word .NET库,它是专门为开发人员设计的用来快 ...

随机推荐

  1. Entity Framework在Asp.net MVC中的实现One Context Per Request(附源码)

    上篇中"Entity Framework中的Identity map和Unit of Work模式", 由于EF中的Identity map和Unit of Work模式,EF体现 ...

  2. informatica 学习日记整理

    1. INFORMATICA CLIENT的使用 1.1 Repository Manager 的使用 1.1.1 创建Repository. 前提: a.在ODBC数据源管理器中新建一个数据源连接至 ...

  3. 编写一个Car类,具有String类型的属性品牌,具有功能drive; 定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速; 定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特 性。

    package com.hanqi.test; public class Car { //构造一个汽车的属性 private String name; //创建getter和setter方法 publ ...

  4. XAML中的特殊符号几空白字符处理

    阅读目录 介绍 详细 处理 Demo下载 介绍 XAML标记语言是基于xml的,所以很多xml中的特殊符号在XAML也是需要处理的. 详细 (取自msdn) 字符 Entity 注释 &(“a ...

  5. BootStrap入门教程 (一)

    BootStrap入门教程 (一)   2011年,twitter的"一小撮"工程师为了提高他们内部的分析和管理能力,用业余时间为他们的产品构建了一套易用.优雅.灵活.可扩展的前端 ...

  6. SQL server基础知识(表操作、数据约束、多表链接查询)

    SQL server基础知识 一.基础知识 (1).存储结构:数据库->表->数据 (2).管理数据库 增加:create database 数据库名称 删除:drop database ...

  7. gulp系列:简单实践

    coffescript测试源码   gulp = require('gulp') #删除 1.清空目录 常用插件 gulp-clean .del (nodejs模块) del = require('d ...

  8. 浴室随想——RogueLike随想

    好玩的RogueLike 0 不同的追求 1 从追求中寻找商机 2 更的直接方法 3 我的追求 4 我的方法 5 好玩的RogueLike RogueLike游戏很好玩,因为你永远不知道接下来会发生什 ...

  9. CentOS系统在不重启的情况下为虚拟机添加新硬盘

    一.概述 用过虚拟机的都知道,如果在系统运行的时候去给虚拟机添加一块新设备,比如说硬盘,系统是读取不到这个新硬盘的,因为系统在启动的时候会去检测硬件设备.但是我们也可能会遇到这样的情况,比如正在运行比 ...

  10. Centos和Redhat的区别和联系

    网上看到的,转载给大家 CentOS与RedHat的关系: RedHat在发行的时候,有两种方式:二进制的发行方式以及源代码的发行方式.无论是哪一种发行方式,你都可以免费获得(例如从网上下载),并再次 ...