DocX学习系列

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

DocX开源WORD操作组件的学习系列二 :  http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_csharp_005_docx2.html

DocX开源WORD操作组件的学习系列三:  http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_csharp_006_docx3.html

DocX开源WORD操作组件的学习系列四:  http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_csharp_006_docx4.html

1 创建目录

1.1创建目录效果图

1.2创建目录代码:

   static void AddToc()
{
Console.WriteLine("\tAddToc()"); using (var document = DocX.Create(@"docs\Toc2.docx"))
{ document.InsertTableOfContents("1 目录", TableOfContentsSwitches.O | TableOfContentsSwitches.U | TableOfContentsSwitches.Z | TableOfContentsSwitches.H, "Heading1");
// document.InsertSectionPageBreak();//分页
var h1 = document.InsertParagraph("2 测试1");
h1.StyleName = "Heading1";
document.InsertParagraph("Some very interesting content here");
var h2 = document.InsertParagraph("3 测试2");
h2.StyleName = "Heading1";
document.InsertParagraph("Some very interesting content here as well");
var h3 = document.InsertParagraph("3.1 测试3.1");
h3.StyleName = "Heading2";
var h4 = document.InsertParagraph("3.1.1 测试3.1.1");
h4.StyleName = "Heading3";
var h5 = document.InsertParagraph("4 测试4");
h5.StyleName = "Heading1";
document.InsertParagraph("Not so very interesting....");
document.Save();
}
}

2.添加书签

2.1添加书签效果图

2.2添加书签代码:

    private static void Bookmarks()
{
Console.WriteLine("\tBookmarks()"); using (var document = DocX.Create(@"docs\Bookmarks.docx"))
{
//添加一个书签
var paragraph = document.InsertBookmark("firstBookmark");
//添加一个段落并填充文本
var paragraph2 = document.InsertParagraph("This is a paragraph which contains a ");
//给段落2尾部添加一个书签,名字为secondBookmark
paragraph2.AppendBookmark("secondBookmark");
//给段落2原有文本的基础上追加文本bookmark
paragraph2.Append("bookmark");
//将段落2的secondBookmark的书签位置插入文本handy
paragraph2.InsertAtBookmark("handy ", "secondBookmark"); paragraph2.ReplaceText("which","WHICH");
//遍历书签
for (var index = ; index < document.Bookmarks.Count; index++)
{
var item = document.Bookmarks[index];
Console.WriteLine(item.Name, item.Paragraph.Text);
}
//修改书签TEXT
for (var index = ; index < document.Bookmarks.Count; index++)
{
var item = document.Bookmarks[ index];
item.SetText("书签" + index);
Console.WriteLine(item.Name, item.Paragraph.Text);
}
document.Save();
Console.WriteLine("\tCreated: docs\\Bookmarks.docx\n"); }
}

3.插入各种分隔符

        static void HelloWorldInsertHorizontalLine()
{
Console.WriteLine("\tHelloWorldInsertHorizontalLine()"); // Create a new document.
using (DocX document = DocX.Create(@"docs\HelloWorldInsertHorizontalLine.docx"))
{
// Insert a Paragraph into this document.
Paragraph p = document.InsertParagraph(); // Append some text and add formatting.
p.Append("Hello World!^011Hello World!")
.Font(new Font("Times New Roman"))
.FontSize()
.Color(WindowsColor.Blue)
.Bold();
p.InsertHorizontalLine("double", , , "auto");
Paragraph p1 = document.InsertParagraph();
p1.InsertHorizontalLine("double", , , "red");
Paragraph p2 = document.InsertParagraph();
p2.InsertHorizontalLine("single", , , "red");
Paragraph p3 = document.InsertParagraph();
p3.InsertHorizontalLine("triple", , , "blue");
Paragraph p4 = document.InsertParagraph();
p4.InsertHorizontalLine("double", , , "red");
// Save this document to disk.
document.Save();
Console.WriteLine("\tCreated: docs\\HelloWorldInsertHorizontalLine.docx\n");
}
}

4.插入超链接

 private static void Hyperlinks()
{
// Create a document.
using (DocX document = DocX.Create(@"docs\Hyperlinks.docx"))
{ // Add a hyperlink into the document.
Hyperlink link1 = document.AddHyperlink("百度一下", new Uri("http://www.baidu.com"));
Hyperlink link2 = document.AddHyperlink("TOC文档",new Uri("file:///" + @"E:\soft\DocX-master\Examples\bin\Debug\docs\Toc2.docx".Replace("\\", "/")));
Uri uri = new Uri("http://www.baidu.com");
Paragraph p1 = document.InsertParagraph(); // Append content to the Paragraph
p1.AppendLine("下面是2个超链接").Append(":").Bold();
p1.AppendLine("百度的: ").AppendHyperlink(link1).Color(WindowsColor.FromArgb(, , )).Append(".");
p1.AppendLine("本地的绝对路径 :").AppendHyperlink(link2).Color(WindowsColor.FromArgb(, , )).Append(".");
p1.AppendLine();
document.Save();
Console.WriteLine("\tCreated: docs\\Hyperlinks.docx\n");
}
}

5.插入分页符合换行符

 private static void BreakPageLine()
{
Console.WriteLine("\tBreakPageLine()");
// Create a new document.
using (DocX document = DocX.Create(@"docs\BreakPageLine.docx"))
{
// Add Headers and Footers to this document. Paragraph p0 = document.InsertParagraph();
p0.Append("Hello First line").Bold();
p0.InsertPageBreakAfterSelf();
var p1 = document.InsertParagraph("zhaojiedi");
document.InsertSectionPageBreak();
document.InsertParagraph("zhaojiedi2");
document.InsertSection();
var p2= document.InsertParagraph("zhaojiedi3");
p2.AppendLine("zhaojiedi4");
p2.AppendLine("zhaojiedi5");
p2.AppendLine("zhaojiedi6");
document.Save();
}// Release this document from memory.
}

6.插入公式

  private static void Equations()
{
Console.WriteLine("\tEquations()");
// Create a new document.
using (DocX document = DocX.Create(@"docs\Equations.docx"))
{
// Insert first Equation in this document.
Paragraph pEquation1 = document.InsertEquation("x = y+z");
// Insert second Equation in this document and add formatting.
Paragraph pEquation2 = document.InsertEquation("x = (y+z)/t").FontSize().Color(WindowsColor.Blue);
// Save this document to disk.
document.Save();
Console.WriteLine("\tCreated: docs\\Equations.docx\n");
}
}

7.插入页眉页脚

        private static void HeadersAndFooters2()
{
Console.WriteLine("\tHeadersAndFooters()"); // Create a new document.
using (DocX document = DocX.Create(@"docs\HeadersAndFooters.docx"))
{
// Add Headers and Footers to this document.
document.AddHeaders();
document.AddFooters(); // Force the first page to have a different Header and Footer.
document.DifferentFirstPage = true; // Force odd & even pages to have different Headers and Footers.
document.DifferentOddAndEvenPages = true; // Get the first, odd and even Headers for this document.
Header header_first = document.Headers.first;
Header header_odd = document.Headers.odd;
Header header_even = document.Headers.even; // Get the first, odd and even Footer for this document.
Footer footer_first = document.Footers.first;
Footer footer_odd = document.Footers.odd;
Footer footer_even = document.Footers.even; // Insert a Paragraph into the first Header.
Paragraph p0 = header_first.InsertParagraph();
p0.Append("Hello First Header.").Bold(); // Insert a Paragraph into the odd Header.
Paragraph p1 = header_odd.InsertParagraph();
p1.Append("Hello Odd Header.").Bold(); // Insert a Paragraph into the even Header.
Paragraph p2 = header_even.InsertParagraph();
p2.Append("Hello Even Header.").Bold(); // Insert a Paragraph into the first Footer.
Paragraph p3 = footer_first.InsertParagraph();
p3.Append("Hello First Footer.").Bold(); // Insert a Paragraph into the odd Footer.
Paragraph p4 = footer_odd.InsertParagraph();
p4.Append("Hello Odd Footer.").Bold(); // Insert a Paragraph into the even Header.
Paragraph p5 = footer_even.InsertParagraph();
p5.Append("Hello Even Footer.").Bold(); // Insert a Paragraph into the document.
Paragraph p6 = document.InsertParagraph();
p6.AppendLine("Hello First page."); // Create a second page to show that the first page has its own header and footer.
p6.InsertPageBreakAfterSelf(); // Insert a Paragraph after the page break.
Paragraph p7 = document.InsertParagraph();
p7.AppendLine("Hello Second page."); // Create a third page to show that even and odd pages have different headers and footers.
p7.InsertPageBreakAfterSelf(); // Insert a Paragraph after the page break.
Paragraph p8 = document.InsertParagraph();
p8.AppendLine("Hello Third page."); //Insert a next page break, which is a section break combined with a page break
document.InsertSectionPageBreak(); //Insert a paragraph after the "Next" page break
Paragraph p9 = document.InsertParagraph();
p9.Append("Next page section break."); //Insert a continuous section break
document.InsertSection(); //Create a paragraph in the new section
var p10 = document.InsertParagraph();
p10.Append("Continuous section paragraph."); // Save all changes to this document.
document.Save(); Console.WriteLine("\tCreated: docs\\HeadersAndFooters.docx\n");
}// Release this document from memory.
}

DocX开源WORD操作组件的学习系列二的更多相关文章

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

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

  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. 开源word操作组件DocX的记录

    开源word操作组件DocX的记录 使用开源word操作组件DocX的记录 1.DocX简介 1.1 简介 DocX是一个在不需要安装word的情况下对word进行操作的开源轻量级.net组件,是由爱 ...

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

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

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

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

  7. [.NET] 开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc

    开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc [博主]反骨仔 [原文地址]http://www.cnblogs.com/li ...

  8. 开源RabbitMQ操作组件

    开源RabbitMQ操作组件 对于目前大多的.NET项目,其实使用的技术栈都是差不多,估计现在很少用控件开发项目的了,毕竟一大堆问题.对.NET的项目,目前比较适合的架构ASP.NET MVC,ASP ...

  9. 图机器学习(GML)&图神经网络(GNN)原理和代码实现(前置学习系列二)

    项目链接:https://aistudio.baidu.com/aistudio/projectdetail/4990947?contributionType=1 欢迎fork欢迎三连!文章篇幅有限, ...

随机推荐

  1. Java是值传递还是引用传递?

    Java的值传递和引用传递在面试中一般都会都被涉及到,今天我们就来聊聊这个问题.这个问题一般是相对函数而言的,也就是Java中所说的方法参数,那么我们先来回顾一下在程序设计语言中有关参数传递给方法的两 ...

  2. CI 框架 隐藏index.php 入口文件 和 设置访问application下子目录

    1.隐藏根目录下 index.php, 在根目录下创建 .htaccess文件 内容如下: <IfModule mod_rewrite.c> RewriteEngine on Rewrit ...

  3. BFS —— 信息学一本通(1451:棋盘游戏)

    题目描述 在一个4*4的棋盘上有8个黑棋和8个白棋,当且仅当两个格子有公共边,这两个格子上的棋是相邻的.移动棋子的规则是交换相邻两个棋子.现在给出一个初始棋盘和一个最终棋盘,要求你找出一个最短的移动序 ...

  4. django默认模板引擎和jinja2模板引擎

    在使用中,大家会发现django默认模板引擎有很多局限性,最明显的就是四则运算.就只能加减,乘除都不支持.另外还有判断相等,不能直接if,要用ifequal.确实不太方便.还有一点,django默认模 ...

  5. [jzoj]5478.【NOIP2017提高组正式赛】列队

    Link https://jzoj.net/senior/#main/show/5478 Description Sylvia 是一个热爱学习的女孩子.       前段时间,Sylvia 参加了学校 ...

  6. java用jsoup解析HTML

    步骤 1获取document对象 //方法一 Document doc = Jsoup.connect(网址).get() //方法二 Document doc = Jsoup.parse(html字 ...

  7. Eclipse自动补全调教

    方法来自http://www.cnblogs.com/sunjie21/archive/2012/06/28/2567463.html 调教后可以做到: 1. sout + Tab 输出System. ...

  8. React修改state(非redux)中数组和对象里边的某一个属性的值

    在使用React时,会经常需要处理state里边设置的初始值以达到我们的实际需求,比如从接口获取到列表数据后要赋值给定义的列表初始值,然后数据驱动view视图进而呈现在我们眼前,这种最简单的赋值方式实 ...

  9. 自己封装element-ui树组件的过滤

    前言:vue开发项目时用到了element-ui的树组件,但是发现一执行过滤事件,树就全部都展开了,为了解决这个问题,只能自己先过滤数剧,再赋值给树组件的data,就避免了一上来全部展开的尴尬. 一. ...

  10. 在IIS上新发布的网站,样式与js资源文件加载不到(资源文件和网页同一个域名下)

    在IIS上新发布的网站,网站能打开,但样式与js资源文件加载不到(资源文件和网页是同一个域名下,例如:网页www.xxx.com/index.aspx,图片www.xxx.com/pic.png). ...