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

代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using DocumentFormat.OpenXml;
  7. using DocumentFormat.OpenXml.Packaging;
  8. using DocumentFormat.OpenXml.Wordprocessing;
  9.  
  10. namespace AddTableToWord
  11. {
  12. public class Program
  13. {
  14. public static void Main(string[] args)
  15. {
  16. List<string[]> lstData = new List<string[]>() { new string[] { "", "", "" }, new string[] { "", "", "" } };
  17. string[] headerArray = new string[] { "A", "B", "C" };
  18. AddTable("Test.docx", lstData, headerArray);
  19. }
  20.  
  21. /// <summary>
  22. /// word里面添加table
  23. /// </summary>
  24. /// <param name="wordPath">word文件路径</param>
  25. /// <param name="lstData">数据</param>
  26. /// <param name="headerArray">表头</param>
  27. public static void AddTable(string wordPath, List<string[]> lstData, string[] headerArray)
  28. {
  29. using (WordprocessingDocument doc = WordprocessingDocument.Open(wordPath, true))
  30. {
  31. TableGrid grid = new TableGrid();
  32. int maxColumnNum = lstData.Select(x => x.Count()).Max();
  33. for (int index = ; index < maxColumnNum; index++)
  34. {
  35. grid.Append(new TableGrid());
  36. }
  37.  
  38. // 设置表格边框
  39. TableProperties tblProp = new TableProperties(
  40. new TableBorders(
  41. new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = },
  42. new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = },
  43. new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = },
  44. new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = },
  45. new InsideHorizontalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = },
  46. new InsideVerticalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = }
  47. )
  48. );
  49.  
  50. Table table = new Table();
  51. table.Append(tblProp);
  52.  
  53. // 添加表头. 其实有TableHeader对象的,小弟用不来.
  54. TableRow headerRow = new TableRow();
  55. foreach (string headerStr in headerArray)
  56. {
  57. TableCell cell = new TableCell();
  58. cell.Append(new Paragraph(new Run(new Text(headerStr))));
  59. headerRow.Append(cell);
  60. }
  61. table.Append(headerRow);
  62.  
  63. // 添加数据
  64. foreach (string[] rowArray in lstData)
  65. {
  66. TableRow row = new TableRow();
  67. foreach (string strCell in rowArray)
  68. {
  69. TableCell cell = new TableCell();
  70. cell.Append(new Paragraph(new Run(new Text(strCell))));
  71. row.Append(cell);
  72. }
  73. table.Append(row);
  74. }
  75.  
  76. doc.MainDocumentPart.Document.Body.Append(new Paragraph(new Run(table)));
  77. }
  78. }
  79. }
  80. }

执行呈现结果如下:

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. 用SQL语句修复SQL Server数据库

    使用数据库的过程中,由于断电或其他原因,有可能导致数据库出现一些小错误,比如检索某些表特别慢,查询不到符合条件的数据等. 出现这些情况的原因,往往是因为数据库有些损坏,或索引不完整. 在ACCESS中 ...

  2. python 的import机制2

    http://blog.csdn.net/sirodeng/article/details/17095591   python 的import机制,以备忘: python中,每个py文件被称之为模块, ...

  3. 配置了<mvc:resources> 导致以前的controller 无法访问。

    解决方案: <mvc:annotation-driven/>

  4. sso demo ( cas )

    1. generate keystore command : keytool -genkey -alias testtomcat -keyalg RSA -keystore "C:\User ...

  5. (六)play之yabe项目【验证码】

    (六)play之yabe项目[验证码] 博客分类: 框架@play framework   添加验证码功能 在Application.java中添加一个action:captcha() /** * 添 ...

  6. IOS缓存机制详解

    资料均来自互联网,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任. 人魔七七:http://www.cnblogs.com/qiqibo/ 为什么要有缓存 应用需要 ...

  7. 管理系统的前端解决方案:Pagurian V1.3发布

    Pagurian 一个管理系统的前端解决方案, 致力于让前端设计,开发,测试,发布更简单. 功能简介 Pagurian 适用于Web管理级的项目 基于Sea.js遵循CMD规范,友好的模块定义,使业务 ...

  8. CSS通过边框border-style来写小三角

    <!DOCTYPE html> /*直接复制代码即可在浏览器验证*/ <html> <head lang="en"> <meta char ...

  9. SharePoint List来做项目管理

    其实这是一个常见的问题,已经不仅仅只是一次用SharePoint List来做项目管理了. 核心 1. SharePoint List Lookup自己来实现项目的父子关系 2. 权限控制,直接控制在 ...

  10. mysql innoDB 挂了的临时解决方案

    Mysql InnoDB: Error: checksum mismatch by Mattias Hemmingsson on December 23, 2013 in Linux • 6 Comm ...