开源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

 
 
标签: DocX

开源word操作组件DocX的记录的更多相关文章

  1. 使用开源word操作组件DocX的记录

    1.DocX简介 1.1 简介 DocX是一个在不需要安装word的情况下对word进行操作的开源轻量级.net组件,是由爱尔兰的一个叫Cathal Coffey的博士生开发出来的.DocX使得操作w ...

  2. DocX开源WORD操作组件的学习系列四

    DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...

  3. DocX开源WORD操作组件的学习系列三

    DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...

  4. DocX开源WORD操作组件的学习系列二

    DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...

  5. DocX开源WORD操作组件的学习系列一

    DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...

  6. 开源Word读写组件DocX 的深入研究和问题总结

    一. 前言 前两天看到了asxinyu大神的[原创]开源Word读写组件DocX介绍与入门,正好我也有类似的自动生成word文档得需求,于是便仔细的研究了这个DocX. 我也把它融入到我的项目当中并进 ...

  7. 开源Word读写组件DocX介绍与入门

    来源:http://i.cnblogs.com/EditPosts.aspx?opt=1 读写Offic格式的文档,大家多少都有用到,可能方法也很多,组件有很多.这里不去讨论其他方法的优劣,只是向大家 ...

  8. C# 开源组件--Word操作组件DocX

    使用模版生成简历 读写表格数据 合并单元格 工具源代码下载 学习使用 使用模版生成简历 下面将以一个简历实例来讲解DocX对表格的操作,先看看生成的效果 private static void Cre ...

  9. 读写Word的组件DocX介绍与入门

    本文为转载内容: 文章原地址:http://www.cnblogs.com/asxinyu/archive/2013/02/22/2921861.html 开源Word读写组件DocX介绍与入门 阅读 ...

随机推荐

  1. [Locked] Binary Tree Longest Consecutive Sequence

    Binary Tree Longest Consecutive Sequence Given a binary tree, find the length of the longest consecu ...

  2. BagTest

    package cn.aust.zyw.demo; import java.util.Iterator; /** * Created by zyw on 2016/2/17. */ public cl ...

  3. selenium webdriver python 操作Chrome浏览器

    Step1: 下载chromedriver. 下载路径: http://chromedriver.storage.googleapis.com/index.html 选择一个合适的下载即可.我下载的是 ...

  4. 低版本Xcode 出现could not find developer disk image问题

    解决Xcode在ipad/iphone9.2系统真机测试时出现could not find developer disk image问题,只要拷贝这个文件(链接: http://pan.baidu.c ...

  5. 详解udev

    如果你使用Linux比较长时间了,那你就知道,在对待设备文件这块,Linux改变了几次策略.在Linux早期,设备文件仅仅是是一些带有适当的属性集的普通文件,它由mknod命令创建,文件存放在/dev ...

  6. solr ,hadoop ,lucene,nutch 的关系和区别

    apache lucene是apache下一个著名的开源搜索引擎内核,基于Java技术,处理索引,拼写检查,点击高亮和其他分析,分词等技术. nutch和solr原来都是lucene下的子项目.但后来 ...

  7. Json序列化、反序列化互换

    // 序列化 using (MemoryStream stream = new MemoryStream()) { serializer.WriteObject(stream, hdm); jsonT ...

  8. Hadoop与HBase中遇到的问题

    1. Hadoop中遇到的问题 曾经所遇到的问题因为没有记录,所以忘了 (1)NameNode没有启动成功, 是因为你对HDFS多次格式化,导致datanode中与namenode中的VERSION文 ...

  9. HDU1700:Points on Cycle

    Problem Description There is a cycle with its center on the origin. Now give you a point on the cycl ...

  10. [Angular 2] Controlling how Styles are Shared with View Encapsulation

    Style and View Encapsulation is best understood by seeing how each option (Emulated, Native, and Non ...