下面将展示如何使用Openxm向Word添加表格. 代码中表头和数据我用的同一个TableRow来添加,其实可以通过TableHeader来,其实都一样。后面教程我会给出如何设置单元格样式。表头那一行可以自己通过设置样式来控制

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing; namespace AddTableToWord
{
public class Program
{
public static void Main(string[] args)
{
List<string[]> lstData = new List<string[]>() { new string[] { "", "", "" }, new string[] { "", "", "" } };
string[] headerArray = new string[] { "A", "B", "C" };
AddTable("Test.docx", lstData, headerArray);
} /// <summary>
/// word里面添加table
/// </summary>
/// <param name="wordPath">word文件路径</param>
/// <param name="lstData">数据</param>
/// <param name="headerArray">表头</param>
public static void AddTable(string wordPath, List<string[]> lstData, string[] headerArray)
{
using (WordprocessingDocument doc = WordprocessingDocument.Open(wordPath, true))
{
TableGrid grid = new TableGrid();
int maxColumnNum = lstData.Select(x => x.Count()).Max();
for (int index = ; index < maxColumnNum; index++)
{
grid.Append(new TableGrid());
} // 设置表格边框
TableProperties tblProp = 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 table = new Table();
table.Append(tblProp); // 添加表头. 其实有TableHeader对象的,小弟用不来.
TableRow headerRow = new TableRow();
foreach (string headerStr in headerArray)
{
TableCell cell = new TableCell();
cell.Append(new Paragraph(new Run(new Text(headerStr))));
headerRow.Append(cell);
}
table.Append(headerRow); // 添加数据
foreach (string[] rowArray in lstData)
{
TableRow row = new TableRow();
foreach (string strCell in rowArray)
{
TableCell cell = new TableCell();
cell.Append(new Paragraph(new Run(new Text(strCell))));
row.Append(cell);
}
table.Append(row);
} doc.MainDocumentPart.Document.Body.Append(new Paragraph(new Run(table)));
}
}
}
}

执行呈现结果如下:

OpenXml入门----给Word文档添加表格的更多相关文章

  1. OpenXml入门----给Word文档添加文字

    使用OpenXml给word文档添加文字,每个模块都有自己对于的属性以及内容,要设置样式就先声明属性对象,将样式Append到属性里面,再将属性append到模块里面,那么模块里面的内容就具备该样式了 ...

  2. C# 给Word文档添加内容控件

    C# 给Word文档添加内容控件 在MS Word中,我们可以通过内容控件来向word文档中插入预先定义好的模块,指定模块的内容格式(如图片.日期.列表或格式化的文本等),从而创建一个结构化的word ...

  3. 向Docx4j生成的word文档添加图片和布局--第一部分

    原文标题:Adding images and layout to your Docx4j-generated word documents, part 1 原文链接:http://blog.iprof ...

  4. Java 如何给Word文档添加多行文字水印

    前言 我在以往的文章中曾介绍过如何给Word文档添加文本水印和图片水印,及怎样删除文档中的水印.关于文本水印,之前那篇教程里主要指的是单行字体的水印,而在操作Word文档时,有时也会碰到需要添加多行文 ...

  5. OpenXml SDK 2.0 创建Word文档 添加页、段落、页眉和页脚

    using (WordprocessingDocument objWordDocument = WordprocessingDocument.Create(@"C:\********.doc ...

  6. C# 操作Word 文档——添加Word页眉、页脚和页码

    在Word文档中,我们可以通过添加页眉.页脚的方式来丰富文档内容.添加页眉.页脚时,可以添加时间.日期.文档标题,文档引用信息.页码.内容解释.图片/LOGO等多种图文信息.同时也可根据需要调整文字或 ...

  7. .NET 动态向Word文档添加数据

    本文章主要用于在网页上填写数据动态填入Word模板中使用 首先要准备一个Word模板,然后在需要插入数据的位置插入书签,这样可以确定在网页上填入的数据可以插入到Word文档相应的位置. 在项目中要声明 ...

  8. C#/VB.NET 给Word文档添加/撤销书签

    在现代办公环境中,阅读或者编辑较长篇幅的Word文档时,想要在文档中某一处或者几处留下标记,方便日后查找.修改时,需要在相对应的文档位置插入书签.那对于开发者而言,在C#或者VB.NET语言环境中,如 ...

  9. 将word文档A表格中的内容拷贝到word文档B表格中

    Function IsFileExists(ByVal strFileName As String) As Boolean ) <> Empty Then IsFileExists = T ...

随机推荐

  1. wcf 双工

    服务器 [ServiceContract(Namespace="http://www.artech.com/", CallbackContract = typeof(ICallba ...

  2. No.011:Container With Most Water

    问题: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  3. asp.net mvc Html.BeginForm()方法

    Html.BeginForm()方法将会输出<form>标签,而且必须以using包起来,如此便可在using程序代码最后退出时,让asp.net mvc帮你补上<form>标 ...

  4. Web.Config的配置

    1.配置数据库连接 在<connectionStrings></connectionStrings>节中完成,配置过程需指定四个属性server(DataSource)服务器名 ...

  5. SharePoint 更新文档库文档标题(Title)字段

    前言:记录下写代码中遇到的小问题,帮同事写一个批量更新文档库标题字段的小程序,本来以为就Update一下就可以了,10分钟可以搞定.结果10分钟过去了,代码写好了,执行起来不报错,调试也没问题,只是要 ...

  6. Sharepoint 2013 列表使用JS Link

    使用JS Link可以向Sharepoint List注册脚本,重写Field模板,使得对于符合条件的字段改变格式和样式.但是有一个问题是,页面postback的话,JS不会被触发,不知道怎么解,有知 ...

  7. teambition的热血团队

    一群热血年轻人开发了一套项目协作软件teambition,无意帮他们宣传,更多见网址www.teambition.com. 网站上简短的一段话,深深的打动了我.他们说teambition: 由热爱工作 ...

  8. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q85-Q87)

    Question 85You create a Microsoft .NET Framework console application that uses a Representational St ...

  9. GCD中的dispatch_set_target_queue的用法及作用

    (一),使用dispatch_set_target_queue更改Dispatch Queue的执行优先级 dispatch_queue_create函数生成的DisPatch Queue不管是Ser ...

  10. IOS之UI--小实例项目--综合使用

    前言: 本博文是基于前一个小实例项目:IOS之UI--小实例项目--添加商品和商品名 进行继续综合学习积累的. 内容大纲 01-综合使用01-plist的使用 02-综合使用02-模型取代字典的好处分 ...