OpenXml入门----给Word文档添加表格
下面将展示如何使用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文档添加表格的更多相关文章
- OpenXml入门----给Word文档添加文字
使用OpenXml给word文档添加文字,每个模块都有自己对于的属性以及内容,要设置样式就先声明属性对象,将样式Append到属性里面,再将属性append到模块里面,那么模块里面的内容就具备该样式了 ...
- C# 给Word文档添加内容控件
C# 给Word文档添加内容控件 在MS Word中,我们可以通过内容控件来向word文档中插入预先定义好的模块,指定模块的内容格式(如图片.日期.列表或格式化的文本等),从而创建一个结构化的word ...
- 向Docx4j生成的word文档添加图片和布局--第一部分
原文标题:Adding images and layout to your Docx4j-generated word documents, part 1 原文链接:http://blog.iprof ...
- Java 如何给Word文档添加多行文字水印
前言 我在以往的文章中曾介绍过如何给Word文档添加文本水印和图片水印,及怎样删除文档中的水印.关于文本水印,之前那篇教程里主要指的是单行字体的水印,而在操作Word文档时,有时也会碰到需要添加多行文 ...
- OpenXml SDK 2.0 创建Word文档 添加页、段落、页眉和页脚
using (WordprocessingDocument objWordDocument = WordprocessingDocument.Create(@"C:\********.doc ...
- C# 操作Word 文档——添加Word页眉、页脚和页码
在Word文档中,我们可以通过添加页眉.页脚的方式来丰富文档内容.添加页眉.页脚时,可以添加时间.日期.文档标题,文档引用信息.页码.内容解释.图片/LOGO等多种图文信息.同时也可根据需要调整文字或 ...
- .NET 动态向Word文档添加数据
本文章主要用于在网页上填写数据动态填入Word模板中使用 首先要准备一个Word模板,然后在需要插入数据的位置插入书签,这样可以确定在网页上填入的数据可以插入到Word文档相应的位置. 在项目中要声明 ...
- C#/VB.NET 给Word文档添加/撤销书签
在现代办公环境中,阅读或者编辑较长篇幅的Word文档时,想要在文档中某一处或者几处留下标记,方便日后查找.修改时,需要在相对应的文档位置插入书签.那对于开发者而言,在C#或者VB.NET语言环境中,如 ...
- 将word文档A表格中的内容拷贝到word文档B表格中
Function IsFileExists(ByVal strFileName As String) As Boolean ) <> Empty Then IsFileExists = T ...
随机推荐
- 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解
[源码下载] 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Toa ...
- C#的回调方法
C# 里面回调方法一般指某个委托.也可以说是接口. using System; using System.Collections.Generic; using System.Linq; using S ...
- Dubbo初探
Dubbo是什么? 1.阿里巴巴开源项目.2.Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. ps: SOA(面相服务的体系结构) RPC( ...
- 二、SQL语句映射文件(1)resultMap
//备注:该博客引自:http://limingnihao.iteye.com/blog/106076 SQL 映射XML 文件是所有sql语句放置的地方.需要定义一个workspace,一般定义为对 ...
- JavaScript来实现打开链接页面(转载)
在页面中的链接除了常规的方式以外,如果使用javascript,还有很多种方式,下面是一些使用javascript,打开链接的几种方式: 1.使用window的open方法打开链接,这里可是在制定页面 ...
- Java的集合框架
01.为什么要使用集合框架? 解析:如果并不知道程序运行时会需要多少对象,或者需要更复杂方式存储对象,那么可以使用Java集合框架. 如果启用集合的删除方法,那么集合中所有元素的索引会自动维护. 集合 ...
- ASP.NET Web API 特性
ASP.NET MVC 4 包含了 ASP.NET Web API, 这是一个创建可以连接包括浏览器.移动设备等多种客户端的 Http 服务的新框架, ASP.NET Web API 也是构建 RES ...
- Android笔记——什么是json?json如何使用?
什么是json 什么是json,json是什么,json如何使用 JSON是JavaScript Object Notation的缩写,可见JSON来源于JavaScript.JSON数据是一系列键值 ...
- 结合微软开放api,使用MSN,Hotmail等登陆Sharepoint网站
成功使用Windows Live账号登陆SharePoint系统. 附上创建SPTrustedIdentityTokenIssuer的PS脚本====================RegSTS.ps ...
- dict和set
#dict和set #dict #Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map #使用键-值(key-value)存储,具有极快的查找速度. #字 ...