1. 程式人生 > >C#采用OpenXml給Word文檔添加表格

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

count nor back XML dev pointer get 設置表格 href

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

這裏將展示如何使用Openxml向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)));
      }
    }
  }
}

執行呈現結果如下:

技術分享圖片

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

除聲明外,跑步客文章均為原創,轉載請以鏈接形式標明本文地址
C#采用OpenXml給Word文檔添加表格

本文地址: http://www.paobuke.com/develop/c-develop/pbk23474.html






相關內容

技術分享圖片C#通過屬性名稱獲取(讀取)屬性值的方法技術分享圖片C#êμ??′????÷1|?üμ?ComboBox技術分享圖片C# 後臺處理圖片的幾種方法技術分享圖片C#代碼操作XML進行增、刪、改操作
技術分享圖片區分WCF與WebService的異同、優勢技術分享圖片C#實現給圖片加水印的方法技術分享圖片C#連接數據庫和更新數據庫的方法技術分享圖片使用aspose.word 第三方的插件實現導出word

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