OpenXml入門----給Word文檔添加表格

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   使用   ar   for   檔案   

        下面將展示如何使用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)));            }        }    }}

執行呈現結果如下:

 

OpenXml入門----給Word文檔添加表格

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.