Docx介绍

官方原文:DocX is a .NET library that allows developers to manipulate Word 2007/2010/2013 files, in an easy and intuitive manner. DocX is fast, lightweight and best of all it does not require Microsoft Word or Office to be installed.(DocX是允许开发者以非常简单的方式操作Word 2007/2010/2013文件的轻量级.NET组件。它的速度非常快,而且不需要安装微软的Office软件。)

Docx特征

DocX组件目前版本的主要特点有:

1)支持在文件中插入、删除和替代文本,支持所有的文本格式,如字体,颜色,大小,斜体字,下划线,删除线,高亮等。

2)支持段落的对齐方式,最新版本支持插入各级标题。

3)支持插入图片、超链接、表格、页眉页脚以及自定义属性等。

Docx实例

官方已经列举了Docx组件几乎所有的功能和实例,这里就不全部说明了。想要了解更多的实例,可以参考官方网站:http://docx.codeplex.com/SourceControl/latest#Examples/Program.cs

使用之前,可以到官网下载最新版本的Docx组件。点击这里下载

实例1:输出支持的所有标题级别

using (DocX document = DocX.Create(@"C:\DocumentHeading.docx"))
{
foreach (HeadingType heading in (HeadingType[])Enum.GetValues(typeof(HeadingType)))
{
string text = string.Format("{0} - The quick brown fox jumps over the lazy dog", heading.EnumDescription()); Paragraph p = document.InsertParagraph();
p.AppendLine(text).Heading(heading);
}
document.Save();
}

实例2:文档中插入表格

Table table1 = document1.AddTable(1, 3); //一行三列
table1.Design = TableDesign.TableGrid; //表格样式
table1.Alignment = Alignment.center; //设置表格居中
table1.Rows[0].Cells[0].Paragraphs[0].Append("列1").Bold();
table1.Rows[0].Cells[1].Paragraphs[0].Append("列2").Bold();
table1.Rows[0].Cells[2].Paragraphs[0].Append("列3").Bold(); table1.Rows[0].Cells[0].Width = 100; //设置单元格宽度
table1.Rows[0].Cells[1].Width = 100;
table1.Rows[0].Cells[2].Width = 100; Paragraph p = document1.InsertParagraph();
p.Alignment = Alignment.center;
p.Append("表格名称").Bold();
p.InsertTableAfterSelf(table1);

实例3:文档中插入图片

Paragraph pPicture = document1.InsertParagraph();
pPicture.Alignment = Alignment.center; Novacode.Image image = document1.AddImage(@"C:\images\1.png); Picture picture = image.CreatePicture();
picture.Width = 240; //设置图片大小
picture.Height = 180;
pPicture.AppendPicture(picture).AppendLine("图片名称").Bold();

实例4:文档中插入文档

DocX document1 = DocX.Create(@"C:\1.docx");
Paragraph p1 = document1.InsertParagraph();
p1.Append("文档document1");
document1.Save(); DocX document2 = DocX.Create(@"C:\2.docx");
Paragraph p2 = document2 .InsertParagraph();
p2.Append("文档document2");
document2.Save(); //文档2插入到文档1
document1.InsertDocument(document2, true);

实例5:根据模板生成文档

1、设置文档模板,模板形式如下图:

2、通过DocX的Load(string path)方法加载模板

3、通过ReplaceText(string seachValue, string newValue)替换模板中标识

//加载模板
DocX document = DocX.Load(@"C:\templateDocx.docx"); //替换文档中标识
document.ReplaceText("$value$", "根据模板生成文档"); //根据表格模板填充表数据
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Column1");
dataTable.Columns.Add("Column2");
dataTable.Rows[0]["Column1"] = "列1内容";
dataTable.Rows[0]["Column2"] = "列2内容";
DataTable resultNew = new DataTable();
resultNew = dataTable.DefaultView.ToTable(false, new string[] { "Column1", "Column2" });
int templateRowId = 1; //从第2列填充数据
Table table = document.Tables[0]; //模板文档中表格索引,从0开始
MakeTableByTemplate(table, resultNew, templateRowId); private void MakeTableByTemplate(Table table, DataTable result, int templateRowId)
{
Row templateRow = table.Rows[templateRowId];
int rowId = 0;
foreach (DataRow row in result.Rows)
{
table.InsertRow(templateRow);
int colId = 0;
foreach (Cell cell in table.Rows[templateRowId + 1 + rowId].Cells)
{
cell.ReplaceText("$d$", row[colId].ToString()); colId++;
}
rowId++;
}
table.RemoveRow(templateRowId);
} //生成文档到指定目录
document.SaveAs(@"C:\sourceFileName.docx");

实例6:文档中生成柱状图

using (DocX document = DocX.Create(@"C:\BarChart.docx"))
{
// Create chart.
BarChart c = new BarChart();
c.BarDirection = BarDirection.Column;
c.BarGrouping = BarGrouping.Standard;
c.GapWidth = 400;
c.AddLegend(ChartLegendPosition.Bottom, false); // Create data.
List<ChartData> company1 = ChartData.CreateCompanyList1();
List<ChartData> company2 = ChartData.CreateCompanyList2(); // Create and add series
Series s1 = new Series("Microsoft");
s1.Color = Color.GreenYellow;
s1.Bind(company1, "Mounth", "Money");
c.AddSeries(s1);
Series s2 = new Series("Apple");
s2.Bind(company2, "Mounth", "Money");
c.AddSeries(s2); // Insert chart into document
document.InsertParagraph("Diagram").FontSize(20);
document.InsertChart(c);
document.Save();
}

  ChartData类

public class ChartData
{
public String Mounth { get; set; }
public Double Money { get; set; } public static List<ChartData> CreateCompanyList1()
{
List<ChartData> company1 = new List<ChartData>();
company1.Add(new ChartData() { Mounth = "January", Money = 100 });
company1.Add(new ChartData() { Mounth = "February", Money = 120 });
company1.Add(new ChartData() { Mounth = "March", Money = 140 });
return company1;
} public static List<ChartData> CreateCompanyList2()
{
List<ChartData> company2 = new List<ChartData>();
company2.Add(new ChartData() { Mounth = "January", Money = 80 });
company2.Add(new ChartData() { Mounth = "February", Money = 160 });
company2.Add(new ChartData() { Mounth = "March", Money = 130 });
return company2;
}
}

  生成结果

同时还可以生成饼图、线形图。

业精于勤,荒于嬉;行成于思,毁于随。

如果你觉得这篇文章不错或者对你有所帮助,可以通过右侧【打赏】功能,给予博主一点点鼓励和支持

Docx组件读写Word文档介绍的更多相关文章

  1. .NET通过调用Office组件导出Word文档

    .NET通过调用Office组件导出Word文档 最近做项目需要实现一个客户端下载word表格的功能,该功能是用户点击"下载表格",服务端将该用户的数据查询出来并生成数据到Word ...

  2. 利用Python-docx 读写 Word 文档中的正文、表格、段落、字体等

    前言: 前两篇博客介绍了 Python 的 docx 模块对 Word 文档的写操作,这篇博客将介绍如何用 docx 模块读取已有 Word 文档中的信息. 本篇博客主要内容有: 1.获取文档的章节信 ...

  3. $用python-docx模块读写word文档

    工作中会遇到需要读取一个有几百页的word文档并从中整理出一些信息的需求,比如产品的API文档一般是word格式的.几百页的文档,如果手工一个个去处理,几乎是不可能的事情.这时就要找一个库写脚本去实现 ...

  4. [转载]java读写word文档,完美解决方案

    做项目的过程中,经常需要把数据里里的数据读出来,经过加工,以word格式输出. 在网上找了很多解决方案都不太理想,偶尔发现了PageOffice,一个国产的Office插件,开发调用非常简单!比网上介 ...

  5. [原创]java读写word文档,完美解决方案

    做项目的过程中,经常需要把数据里里的数据读出来,经过加工,以word格式输出. 在网上找了很多解决方案都不太理想,偶尔发现了PageOffice,一个国产的Office插件,开发调用非常简单!比网上介 ...

  6. Python用python-docx读写word文档

    python-docx库可用于创建和编辑Microsoft Word(.docx)文件.官方文档:https://python-docx.readthedocs.io/en/latest/index. ...

  7. BCB 读写Word文档

    void __fastcall TForm1::btn1Click(TObject *Sender) { Variant WordApp,WordDocs,WordDoc; Variant word_ ...

  8. python读写word文档

    读: from docx import Document dir_docx = 'F:\Eclipse\workspace\Spider\cnblogs_doc\mytest - 副本.docx' d ...

  9. C#开源组件DocX处理Word文档基本操作(二)

    上一篇 C#开源组件DocX处理Word文档基本操作(一) 介绍了DocX的段落.表格及图片的处理,本篇介绍页眉页脚的处理. 示例代码所用DocX版本为:1.3.0.0.关于版本的区别,请参见上篇,而 ...

随机推荐

  1. PHP编码规范实例

    <?php   /**   * 符合psr-1,2的编程实例   *   * @author 作者 描述   */       namespace Standard; // 顶部命名空间   / ...

  2. JavaScript 中的 this 问题总结 !

    2016-12-28 vvv阿城 JavaScript 转自  https://qiutc.me/post/this-this-this-in-javascript.html#call,_apply, ...

  3. 用js实现文字提示层 ---总结

    文字提示层在项目中应该是比较常见的,我工作中项目中就用到了,原理是一样,只不过形式不一样,今天通过看视频学习,学会了,总结下: 首先,页面效果如下:  需求: 当鼠标移入到红色字体是,提示框会显示在下 ...

  4. JS 基础学习随想

    2012年就已经接触过了js,给我的印象:这是一门谈不上复杂的语言.大概这就是所谓的学的越浅,用的越少,觉得自己会的东西好像得更多吧!开始做基础练习题的时候觉得好像都十分简单.可是后来在做到对象数组的 ...

  5. [Selenium With C#学习笔记] Lesson-01环境搭建

    Step-1:准备所需的开发环境.浏览器驱动.Selenium-Webdriver.单元测试框架,因目前使用C#的开发神器都Visual Studio,本文也打算采用Visual Studio 201 ...

  6. Animate自定义动画

    在jQuery中出了基本的动画之外,还有用户 可以自定义的函数,Animate() 用于创建自定义动画的函数. apI上指出: 这个函数的关键在于指定动画形式及结果样式属性对象.这个对象中每个属性都表 ...

  7. Redis【第一篇】安装

    第一步:准备 1. 操作系统 CentOS-7-x86_64-Everything-1511 2. redis 版本 redis-3.2.8 3. 修改内核参数 有三种方式: 1)编辑/etc/sys ...

  8. eclipse+HBASE开发环境搭建(已实践)

    开发准备: jdk1.8.45 hbase-1.2.2(windows下和linux个留一份) hadoop-2.7.2(linux一份) Linux系统(centos或其它) Hadoop安装环境 ...

  9. Spring Cache扩展:注解失效时间+主动刷新缓存

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  10. 基于Struts自定义MVC-1

    自定义MVC        数据库:Oracle表:User(id,uname,upwd)自定义Struts框架一.定义Action接口 1 import javax.servlet.http.*; ...