将表格添加到Word文档中 ,包括表格样式设置
创建 Table 对象并设置其属性
在您将表格插入文档之前,必须创建 Table 对象并设置其属性。 要设置表格的属性,请创建TableProperties对象并为其提供值。 TableProperties 类提供许多面向表格的属性,例如Shading 、TableBorders 、TableCaption 、TableCellSpacing 、TableJustification 等等。 示例方法包括以下代码。
Table table = new Table();
TableProperties props = new TableProperties(
new TableBorders(
new TopBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size =
},
new BottomBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size =
},
new LeftBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size =
},
new RightBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size =
},
new InsideHorizontalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size =
},
new InsideVerticalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size =
}));
table.AppendChild<TableProperties>(props);
TableProperties 类的构造函数允许您指定任意数量的子元素(与 XElement 构造函数很像)。
本例中,代码创建TopBorder 、BottomBorder 、LeftBorder 、RightBorder 、InsideHorizontalBorder 和InsideVerticalBorder 子元素,各子元素分别描述表格一个的边框元素。 对于每个元素,代码在调用构造函数的过程中设置 Val 和 Size 属性。 大小的设置很简单,但是 Val 属性的设置则复杂一点:此特定对象的这个属性表示边框样式,您必须将其设置为枚举值。 为此,请创建 EnumValue 泛型类型的实例,将特定边框类型(Single )作为参数传递给构造函数。< >一旦代码已设置它需要设置的所有表格边框值,它将调用 AppendChild<T> 方法的表,指示泛型类型是 TableProperties— 即使用变量 属性 作为值追加 TableProperties 类的实例。
使用数据填充表
有了表格及其属性后,现在应该使用数据填充表格。示例过程首先循环访问您指定的字符串数组中的所有数据行,并为每个数据行创建一个新的 TableRow 实例。下面的代码省去了使用数据填充行的细节,但是演示了如何创建行并将行附加到表格中:
创建11行
for (var i = ; i <=; i++)
{
var tr = new TableRow();
// Code removed here…
table.Append(tr);
}
对于每一列,代码创建一个新的 TableCell 对象,并使用数据填充,然后将其附加到行。下面的代码省去了使用数据填充每个单元格的细节,但是演示了如何创建列并将列附加到表格中:
创建11列
for (var j = ; j <= ; j++)
{
var tc = new TableCell();
// Code removed here…
tr.Append(tc);
}
接下来,代码执行以下操作:
新建一个包含字符串中的值的 Text 对象。
将 Text 对象传递给新Run 对象的构造函数。
将 Run 对象传递给Paragraph 对象的构造函数。
将 Paragraph 对象传递给单元格的Append 方法。
换句话说,下面的代码将文本附加到新的 TableCell 对象。
tc.Append(new Paragraph(new Run(new Text("数据")));
代码然后将新的 TableCellProperties 对象附加到单元格。 与见过的 TableProperties 对象一样,此 TableCellProperties 对象的构造函数可以接受您提供的任意数量的对象。在本例中,代码仅传递了一个新的TableCellWidth 对象,并将其Type 属性设置为Auto (以便表格自动调整每列的宽度)。
// Assume you want columns that are automatically sized.
tc.Append(new TableCellProperties(
new TableCellWidth { Type = TableWidthUnitValues.Auto }));
完成
下面的代码最后会将表格附加到文档正文,然后保存该文档。
doc.Body.Append(table);
doc.Save();
示例代码
// Take the data from a two-dimensional array and build a table at the
// end of the supplied document.
public static void AddTable(string fileName, string[,] data)
{
using (var document = WordprocessingDocument.Open(fileName, true))
{ var doc = document.MainDocumentPart.Document; Table table = new Table(); TableProperties props = new TableProperties(
new TableBorders(
new TopBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size =
},
new BottomBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size =
},
new LeftBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size =
},
new RightBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size =
},
new InsideHorizontalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size =
},
new InsideVerticalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size =
})); table.AppendChild<TableProperties>(props); for (var i = ; i <= data.GetUpperBound(); i++)
{
var tr = new TableRow();
for (var j = ; j <= data.GetUpperBound(); j++)
{
var tc = new TableCell();
tc.Append(new Paragraph(new Run(new Text(data[i, j])))); // Assume you want columns that are automatically sized.
tc.Append(new TableCellProperties(
new TableCellWidth { Type = TableWidthUnitValues.Auto })); tr.Append(tc);
}
table.Append(tr);
}
doc.Body.Append(table);
doc.Save();
}
}
将表格添加到Word文档中 ,包括表格样式设置的更多相关文章
- C# 在word文档中复制表格并粘帖到下一页中
C# 在word文档中复制表格并粘帖到下一页中 object oMissing = System.Reflection.Missing.Value; Microsoft.Offi ...
- 使用Java POI来选择提取Word文档中的表格信息
通过使用Java POI来提取Word(1992)文档中的表格信息,其中POI支持不同的ms文档类型,在具体操作中需要注意.本文主要是通过POI来提取微软2003文档中的表格信息,具体code如下(事 ...
- 2018-10-04 [日常]用Python读取word文档中的表格并比较
最近想对某些word文档(docx)的表格内容作比较, 于是找了一下相关工具. 参考Automate the Boring Stuff with Python中的word部分, 试用了python-d ...
- [java 2019-04-09] 代码生成word文档中的表格嵌套问题
public static void createContent3(Date adtStart, Date adtEnd, Map<String, Object> aMap,Map< ...
- 处理Word文档中所有修订
打开现有文档进行编辑 若要打开现有文档,您可以将 Word类实例化,如以下 using 语句所示. 为此,您可以使用Open(String, Boolean) 方法打开具有指定 fileName 的字 ...
- Aspose.Words:如何添加另一个WORD文档中的Node对象
原文:Aspose.Words:如何添加另一个WORD文档中的Node对象 首先看一段代码,这段代码意图从docSource中获取第一个表格,并插入docTarget的末尾: , true); doc ...
- 向Docx4j生成的word文档中添加布局--第二部分
原文标题:Adding layout to your Docx4j-generated word documents, part 2 原文链接:http://blog.iprofs.nl/2012/1 ...
- 如何在word文档中添加mathtype加载项
MathType是强大的数学公式编辑器,通常与office一起使用,mathtype安装完成后,正常情况下会在word文档中的菜单中自动添加mathtype加载项,但有时也会出现小意外,mathtyp ...
- 利用Python-docx 读写 Word 文档中的正文、表格、段落、字体等
前言: 前两篇博客介绍了 Python 的 docx 模块对 Word 文档的写操作,这篇博客将介绍如何用 docx 模块读取已有 Word 文档中的信息. 本篇博客主要内容有: 1.获取文档的章节信 ...
随机推荐
- 一个简单的struts2上传图片的例子
https://www.cnblogs.com/yeqrblog/p/4398914.html 在我的大创项目中有对应的应用
- css的继承和层叠
标签(空格分隔): css css称为层叠样式表,CSS有两大特性:继承性和层叠性,本章简单介绍一下继承性: 继承性: 定义:继承就是给父及设置了一些属性,子级继承了父及的该属性,这就是我们的css的 ...
- python note 15 正则表达式
# 正则表达式 只和字符串打交道 # 正则表达式的规则# 规则 字符串 从字符串中找到符合规则的内容 # 字符组 : [] 写在中括号中的内容,都出现在下面的某一个字符的位置上都是符合规则的 # [0 ...
- Scrapy中集成selenium
面对众多动态网站比如说淘宝等,一般情况下用selenium最好 那么如何集成selenium到scrapy中呢? 因为每一次request的请求都要经过中间件,所以写在中间件中最为合适 from se ...
- tiny4412 --Uboot移植(6) SD卡驱动,启动内核
开发环境:win10 64位 + VMware12 + Ubuntu14.04 32位 工具链:linaro提供的gcc-linaro-6.1.1-2016.08-x86_64_arm-linux-g ...
- navicat使用ssh登录mysql报错:expected key exchange group packet from server
转载自:https://blog.csdn.net/enweitech/article/details/80677374 解决方法: vim /etc/ssh/sshd_config shift+g ...
- python 将mysql数据库中的int类型修改为NULL 报1366错误,解决办法
gt.run_sql()是用pymysql 封装的类 distribution_sort_id type: int目的:将此字段值全部修改为NULL g=2gt.run_sql("updat ...
- freeze
当我们开发项目的时候,会用virtualenv创建很多python独立环境这时候会出现在不同环境下安装相同的模块的情况,为了避免我们通过联网所需模块,不如我们直接从之前Python环境已有的模块中直接 ...
- hbase-bloom filter
bloom fliter的作用主要用于提升hbase的读性能,但是会牺牲一定的存储空间. 原理: bloom fliter是一种空间效率很高的随机数据结构,初始状态时,bloom filter是一个包 ...
- webpack 打包问题
Project is running at http://localhost:8080/webpack output is served from /dist/webpack: wait until ...