下面将展示如何使用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[] { "1", "2", "3" }, new string[] { "3", "2", "1" } };
            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 = 0; index < maxColumnNum; index++)
                {
                    grid.Append(new TableGrid());
                }

                // 设置表格边框
                TableProperties tblProp = new TableProperties(
                new TableBorders(
                new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 2 },
                new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 2 },
                new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 2 },
                new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 2 },
                new InsideHorizontalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 2 },
                new InsideVerticalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 2 }
                )
                );

                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)));
            }
        }
    }
}
复制代码

执行呈现结果如下:

 

posted on   FrankZC  阅读(3854)  评论(1编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?



点击右上角即可分享
微信分享提示