国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#采用OpenXml給Word文檔添加表格

2020-01-24 02:23:39
字體:
供稿:網(wǎng)友

本文實例講述了C#采用OpenXml給Word文檔添加表格的方法,是非常實用的操作技巧。分享給大家供大家參考。具體分析如下:

這里將展示如何使用Openxml向Word添加表格. 代碼中表頭和數(shù)據(jù)我們用的同一個TableRow來添加,其實可以通過TableHeader來,其實都一樣。后面我們還會進(jìn)一步給出如何設(shè)置單元格樣式。表頭那一行可以自己通過設(shè)置樣式來控制

示例代碼如下:

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">數(shù)據(jù)</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());        }        // 設(shè)置表格邊框        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);        // 添加數(shù)據(jù)        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)));      }    }  }}

執(zhí)行呈現(xiàn)結(jié)果如下:

希望本文所述對大家的C#程序設(shè)計有所幫助

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 出国| 吉水县| 永泰县| 盐亭县| 华亭县| 鄂尔多斯市| 攀枝花市| 定安县| 温州市| 黔南| 桃园县| 嘉鱼县| 满城县| 霍邱县| 隆安县| 车险| 丽水市| 嘉兴市| 澎湖县| 庄河市| 千阳县| 南宁市| 永修县| 庄河市| 德州市| 肥东县| 咸宁市| 黄石市| 响水县| 中江县| 涪陵区| 犍为县| 安西县| 东安县| 延川县| 富顺县| 庆元县| 济源市| 连云港市| 建水县| 锦屏县|