亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2014年09月24日 10:27:21   投稿:shichen2014  
這篇文章主要介紹了C#采用OpenXml給Word文檔添加表格的方法,是OpenXml操作Word的一個(gè)非常實(shí)用的技巧,需要的朋友可以參考下

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

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

示例代碼如下:

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);

        // 添加表頭. 其實(shí)有TableHeader對(duì)象的,小弟用不來(lái).
        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é)果如下:

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

相關(guān)文章

  • C#執(zhí)行DOS命令的方法

    C#執(zhí)行DOS命令的方法

    這篇文章主要介紹了C#執(zhí)行DOS命令的方法,涉及針對(duì)進(jìn)程的調(diào)用以及系統(tǒng)DOS命令的使用,具有不錯(cuò)的實(shí)用價(jià)值,需要的朋友可以參考下
    2014-11-11
  • C# List的賦值問(wèn)題的解決

    C# List的賦值問(wèn)題的解決

    本文主要介紹了C# List的賦值問(wèn)題的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • c# 面試必備線程基礎(chǔ)知識(shí)點(diǎn)

    c# 面試必備線程基礎(chǔ)知識(shí)點(diǎn)

    這篇文章主要介紹了c# 面試必備線程基礎(chǔ)知識(shí)點(diǎn),幫助大家更好的鞏固,掌握線程的基礎(chǔ)知識(shí),感興趣的朋友可以了解下
    2020-11-11
  • WPF+SkiaSharp實(shí)現(xiàn)自繪拖曳小球

    WPF+SkiaSharp實(shí)現(xiàn)自繪拖曳小球

    WPF的拖曳效果,基本配置一下,就可以了,但是自繪的話,就得自己控制。本文將利用WPF+SkiaSharp實(shí)現(xiàn)自繪拖曳小球,感興趣的可以動(dòng)手嘗試一下
    2022-07-07
  • C#生成隨機(jī)ArrayList的方法

    C#生成隨機(jī)ArrayList的方法

    這篇文章主要介紹了C#生成隨機(jī)ArrayList的方法,實(shí)例分析了C#中ArrayList的相關(guān)操作技巧,需要的朋友可以參考下
    2015-06-06
  • C#正則匹配RegexOptions選項(xiàng)的組合使用方法

    C#正則匹配RegexOptions選項(xiàng)的組合使用方法

    本文主要簡(jiǎn)單介紹RegexOptions各種選項(xiàng)的作用,并介紹如何組合使用,為初學(xué)者解除一些疑惑。
    2016-04-04
  • 使用 C# 下載文件的多種方法小結(jié)

    使用 C# 下載文件的多種方法小結(jié)

    本文從最簡(jiǎn)單的下載方式開(kāi)始步步遞進(jìn),講述了文件下載過(guò)程中的常見(jiàn)問(wèn)題并給出了解決方案。并展示了如何使用多線程提升 HTTP 的下載速度以及調(diào)用 aria2 實(shí)現(xiàn)非 HTTP 協(xié)議的文件下載,對(duì)C# 下載文件相關(guān)知識(shí)感興趣的朋友一起看看吧
    2021-08-08
  • C# 使用GDI繪制雷達(dá)圖的實(shí)例

    C# 使用GDI繪制雷達(dá)圖的實(shí)例

    這篇文章主要介紹了C# 使用GDI繪制雷達(dá)圖,本文通過(guò)一段實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-11-11
  • 深入解析C#中的abstract抽象類(lèi)

    深入解析C#中的abstract抽象類(lèi)

    這篇文章主要介紹了深入解析C#中的abstract抽象類(lèi),包括定義抽象類(lèi)等C#面相對(duì)象編程中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2016-01-01
  • C# 使用 OleDbConnection 連接讀取Excel的方法

    C# 使用 OleDbConnection 連接讀取Excel的方法

    這篇文章主要介紹了C# 使用 OleDbConnection 連接讀取Excel的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12

最新評(píng)論