开源word操作组件DocX的记录
开源word操作组件DocX的记录
使用开源word操作组件DocX的记录
1.DocX简介
1.1 简介
DocX是一个在不需要安装word的情况下对word进行操作的开源轻量级.net组件,是由爱尔兰的一个叫Cathal Coffey的博士生开发出来的。DocX使得操作word非常轻便,有利于减轻开发负担,提升程序效率。DocX在Codeplex和Github上都有开源。
1.2 获取与安装
可以在http://docx.codeplex.com/releases下载获取,也可以直接利用NuGet获取。
Install-Package DocX
1.3 开发环境
用DocX需要.NET framework4.0和VS2010或更高版本。
2.DocX相关常用操作(持续更新中...)
2.1 创建word文档
DocX document = DocX.Create(@"docs\HelloWorld.docx")
2.2 加载word文档
DocX document = DocX.Load(@"docs\HelloWorld.docx")
2.3 书签相关操作
2.3.1 插入书签
var paragraph = document.InsertBookmark("firstBookmark");
2.3.2 根据书签名获取书签
如果知道一个书签的书签名,可以直接得到。
var b = document.Bookmarks["书签1"];
2.3.3 在书签中插入文字
document.Bookmarks["书签1"].SetText("Hello World!");
2.3.4 在书签中插入图片、表格
document.Bookmarks["书签2"].Paragraph.InsertPicture(@"pic.jpg");
document.Bookmarks["书签3"].Paragraph.InsertTableAfterSelf(t);//t是Table类型
2.4 分节符和分页符
2.4.1 分节符
document.InsertSectionPageBreak();//分节符
2.4.2 分页符
Paragraph p = document.InsertParagraph();
p.InsertPageBreakAfterSelf();//分页符
2.5 添加目录

1 static void AddToc()
2 {
3 Console.WriteLine("\tAddToc()");
4
5 using (var document = DocX.Create(@"docs\Toc.docx"))
6 {
7 document.InsertTableOfContents("目录", TableOfContentsSwitches.O | TableOfContentsSwitches.U | TableOfContentsSwitches.Z | TableOfContentsSwitches.H, "Heading2");
8 var h1 = document.InsertParagraph("Heading 1");
9 h1.StyleName = "Heading1";
10 document.InsertParagraph("Some very interesting content here");
11 var h2 = document.InsertParagraph("Heading 2");
12 document.InsertSectionPageBreak();
13 h2.StyleName = "Heading1";
14 document.InsertParagraph("Some very interesting content here as well");
15 var h3 = document.InsertParagraph("Heading 2.1");
16 h3.StyleName = "Heading2";
17 document.InsertParagraph("Not so very interesting....");
18
19 document.Save();
20 }
21 }

2.6 插入图片
Image img = document.AddImage(@"pic.jpg");
Picture pic = img.CreatePicture();
Paragraph p1 = document.InsertParagraph();
p1.InsertPicture(pic);
2.7 操作表格
2.7.1 创建和插入表格
Table t = document.AddTable(3, 4);//三行四列
2.7.2 单元格合并
Table t = document.AddTable(3,4);
t.MergeCellsInColumn(0, 0, 1);//public void MergeCellsInColumn(int columnIndex, int startRow, int endRow);竖向合并
t.Rows[0].MergeCells(1, 2);//public void MergeCells(int startIndex, int endIndex);横向合并
注:合并单元格的时候注意,最好先竖向合并,再横向合并,以免报错,因为横向合并会改变列数。
3. 资源
开源网址:http://docx.codeplex.com/(里面的示例代码很适合初学者学习)
高质量博客推荐:http://www.cnblogs.com/asxinyu/archive/2013/02/22/2921861.html#_label3
利用DocX操作word的开源小项目:https://github.com/hahahuahai/create-word-by-DocX
开源word操作组件DocX的记录的更多相关文章
- 使用开源word操作组件DocX的记录
1.DocX简介 1.1 简介 DocX是一个在不需要安装word的情况下对word进行操作的开源轻量级.net组件,是由爱尔兰的一个叫Cathal Coffey的博士生开发出来的.DocX使得操作w ...
- DocX开源WORD操作组件的学习系列四
DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...
- DocX开源WORD操作组件的学习系列三
DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...
- DocX开源WORD操作组件的学习系列二
DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...
- DocX开源WORD操作组件的学习系列一
DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...
- 开源Word读写组件DocX 的深入研究和问题总结
一. 前言 前两天看到了asxinyu大神的[原创]开源Word读写组件DocX介绍与入门,正好我也有类似的自动生成word文档得需求,于是便仔细的研究了这个DocX. 我也把它融入到我的项目当中并进 ...
- 开源Word读写组件DocX介绍与入门
来源:http://i.cnblogs.com/EditPosts.aspx?opt=1 读写Offic格式的文档,大家多少都有用到,可能方法也很多,组件有很多.这里不去讨论其他方法的优劣,只是向大家 ...
- C# 开源组件--Word操作组件DocX
使用模版生成简历 读写表格数据 合并单元格 工具源代码下载 学习使用 使用模版生成简历 下面将以一个简历实例来讲解DocX对表格的操作,先看看生成的效果 private static void Cre ...
- 读写Word的组件DocX介绍与入门
本文为转载内容: 文章原地址:http://www.cnblogs.com/asxinyu/archive/2013/02/22/2921861.html 开源Word读写组件DocX介绍与入门 阅读 ...
随机推荐
- 「书评」SAP内存计算——HANA
因为工作关系,长期跟SAP打交道,所以去年就对HANA有了一些了解,只是公司目前的应用规模还较小,暂时没有上马HANA的打算,但是提前作一些学习还是很有必要的.正好清华大学出版社最近出版了这本< ...
- 通配符的匹配很全面, 但无法找到元素 'cache:advice' 的声明
EB-INF\classes\spring-jdbc.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineN ...
- 表格布局TableLayout
简单实现计算机界面布局的案例: <?xml version="1.0" encoding="utf-8"?><TableLayout xmln ...
- Python随机数与随机字符串详解
随机整数:>>>importrandom>>>random randint(0,99)21随机选取0到100间的偶数:>>>importrando ...
- hdu4010 Query On The Trees
Problem Description We have met so many problems on the tree, so today we will have a query problem ...
- javaScript特效
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- HTML5 类型数组TypeArray(一)
1.起源 TypedArray是一种通用的固定长度缓冲区类型,允许读取缓冲区中的二进制数据. 其在WEBGL规范中被引入用于解决Javascript处理二进制数据的问题. TypedArray已经被大 ...
- (转)PHP自定义遍历目录下所有文件dir(),readdir()函数
方法一:使用dir()遍历目录 dir()函数,成功时返回Directory类实例 PHP dir() 语法格式为: dir(directory);//directory为需要显示文件名的目录名称,可 ...
- xmlns:android="http://schemas.android.com/apk/res/android" 是什么意思?
声明xml命名空间.xmlns意思为“xml namespace”.冒号后面是给这个引用起的别名.schemas是xml文档的两种约束文件其中的一种,规定了xml中有哪些元素(标签).元素有哪些属性及 ...
- class-loader.
the jdk hierarchical relationship of class-loader ----Module Class Loading and Bootstrapping---- boo ...