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

首頁 > 編程 > C# > 正文

OpenXml合并Table單元格代碼實例

2020-01-24 00:01:43
字體:
來源:轉載
供稿:網友
using DocumentFormat.OpenXml;using DocumentFormat.OpenXml.Packaging;using DocumentFormat.OpenXml.Wordprocessing;using OpenXML.Model;using System;using System.Collections.Generic;namespace OpenXML{  class Program  {    //表格數據    public static List<List<string>> _tabData;    public Program(List<List<string>> tabData) {      _tabData = tabData;    }    static void Main(string[] args)    {      List<string> dataTitle = new List<string>() { "序號","姓名","性別"};      List<string> data1 = new List<string>() { "1", "張三", "男" };      List<string> data2 = new List<string>() { "2", "王五", "男" };      List<string> data3 = new List<string>() { "3", "李四", "女" };      _tabData = new List<List<string>>();      _tabData.Add(dataTitle);      _tabData.Add(data1);      _tabData.Add(data2);      _tabData.Add(data3);      CreateTable(_tabData, @"C:/Users/dzw159/Desktop/WT/VS/OpenXMLFile/openXMLTest.docx",300);      //CreateOpenXMLFile(@"C:/Users/dzw159/Desktop/WT/VS/OpenXMLFile/openXMLTest.docx");      Console.WriteLine("Hello World!");      Console.Read();    }    /// <summary>    /// 創建Word    /// </summary>    /// <param name="filePath"></param>    public static void CreateOpenXMLFile(string filePath)    {      using (WordprocessingDocument objWordDocument = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document))      {        MainDocumentPart objMainDocumentPart = objWordDocument.AddMainDocumentPart();        objMainDocumentPart.Document = new Document(new Body());        Body objBody = objMainDocumentPart.Document.Body;        //創建一些需要用到的樣式,如標題3,標題4,在OpenXml里面,這些樣式都要自己來創建的          //ReportExport.CreateParagraphStyle(objWordDocument);         SectionProperties sectionProperties = new SectionProperties();        PageSize pageSize = new PageSize();        PageMargin pageMargin = new PageMargin();        Columns columns = new Columns() { Space = "220" };//720         DocGrid docGrid = new DocGrid() { LinePitch = 100 };//360                                   //創建頁面的大小,頁距,頁面方向一些基本的設置,如A4,B4,Letter,                                    //GetPageSetting(PageSize,PageMargin);         //在這里填充各個Paragraph,與Table,頁面上第一級元素就是段落,表格.         objBody.Append(new Paragraph());        objBody.Append(new Table());        objBody.Append(new Paragraph());        //我會告訴你這里的順序很重要嗎?下面才是把上面那些設置放到Word里去.(大家可以試試把這下面的代碼放上面,會不會出現打開openxml文件有誤,因為內容有誤)         sectionProperties.Append(pageSize, pageMargin, columns, docGrid);        objBody.Append(sectionProperties);        //如果有頁眉,在這里添加頁眉.         //if (IsAddHead)        //{          //添加頁面,如果有圖片,這個圖片和上面添加在objBody方式有點不一樣,這里搞了好久.           //ReportExport.AddHeader(objMainDocumentPart, image);         //}        objMainDocumentPart.Document.Save();      }    }    /// <summary>    /// 創建Tab    /// </summary>    /// <param name="tabData"></param>    /// <param name="filePath"></param>    /// <param name="width"></param>    public static void CreateTable(List<List<string>> tabData, string filePath,int width)    {      //打開Word文件      using (WordprocessingDocument d = WordprocessingDocument.Open(filePath,true))      {        //聲明表格        Table tab = new Table();        //表格樣式        TableProperties tblProp = new TableProperties(new TableBorders(          new TableBorders(            new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single),Size = 4},             new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 4 },            new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 4 },            new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 4 },            new InsideHorizontalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 4 },            new InsideVerticalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 4 }          )        ));        //設置表格寬度        tblProp.TableWidth = new TableWidth() { Width = width.ToString(), Type = TableWidthUnitValues.Dxa };        //樣式添加        tab.Append(tblProp);        int j = 0;        //循環生成單元格        foreach (var item in tabData)        {          //聲明Tab行          TableRow row = new TableRow();          for (var i = 0; i < item.Count; i++)          {            //申明單元格            TableCell cell = new TableCell();            TableCellProperties tableCellProperties = new TableCellProperties();            //單元格樣式設置            TableCellMargin margin = new TableCellMargin();            margin.LeftMargin = new LeftMargin() {              Width="100",              Type = TableWidthUnitValues.Dxa            };            margin.RightMargin = new RightMargin()            {              Width = "100",              Type = TableWidthUnitValues.Dxa            };            tableCellProperties.Append(margin);            Paragraph par = new Paragraph();            Run run = new Run();            //如果同一列的參數相同(合并單元格)            if (j < (tabData.Count - 1) && item[i] == tabData[j + 1][i])            {              VerticalMerge verticalMerge = new VerticalMerge() {                Val = MergedCellValues.Restart              };              //RunProperties rpr = new RunProperties();              //rpr.Append(new Bold());              //run.Append(rpr);              //verticalMerge.Val = MergedCellValues.Restart;              //Text t = new Text(item[i]);              //t.Space = new EnumValue<SpaceProcessingModeValues>(SpaceProcessingModeValues.Preserve);              //run.Append(t);              TableCellProperties tableCellProperties2 = new TableCellProperties();              tableCellProperties2.Append(verticalMerge);              cell.Append(tableCellProperties2);              Text t = new Text(item[i]);              t.Space = new EnumValue<SpaceProcessingModeValues>(SpaceProcessingModeValues.Preserve);              run.Append(t);            }            //和上一行比較(合并單元格)            else if (j>0 && j < (tabData.Count) && item[i] == tabData[j -1][i])            {              VerticalMerge verticalMerge = new VerticalMerge()              {                Val = MergedCellValues.Continue              };              TableCellProperties tableCellProperties2 = new TableCellProperties();              tableCellProperties2.Append(verticalMerge);              cell.Append(tableCellProperties2);              Text t = new Text(item[i]);              t.Space = new EnumValue<SpaceProcessingModeValues>(SpaceProcessingModeValues.Preserve);              run.Append(t);            }            else            {              //RunProperties rPr = new RunProperties();              //rPr.Append(new Bold());              //run.Append(rPr);              //單元格內容添加(由內向外順序)              Text t = new Text(item[i]);              t.Space = new EnumValue<SpaceProcessingModeValues>(SpaceProcessingModeValues.Preserve);              run.Append(t);            }            par.Append(run);            cell.Append(tableCellProperties);            cell.Append(par);            row.Append(cell);          }          j++;          //表格添加行          tab.Append(row);        }        //objBody.Append(new Paragraph());        //objBody.Append(new Table());                d.MainDocumentPart.Document.Body.Append(new Paragraph(new Run(tab)));        d.MainDocumentPart.Document.Save();      }    }      }}

注:他們有的說,標記為MergedCellValues.Continue的縱向單元格一定要給值!(這個我試著給賦值null或者為“”,都能正常合并)

以上就是全部知識點內容,感謝大家的閱讀和對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高陵县| 府谷县| 句容市| 五台县| 沅陵县| 南岸区| 安龙县| 安塞县| 营山县| 临泉县| 河东区| 连城县| 白河县| 五寨县| 正镶白旗| 崇州市| 筠连县| 揭东县| 景泰县| 保德县| 托里县| 马山县| 德安县| 晋城| 开江县| 淮北市| 麦盖提县| 天台县| 大关县| 乌拉特中旗| 疏附县| 读书| 勃利县| 竹溪县| 江安县| 双城市| 古蔺县| 凤冈县| 和龙市| 海口市| 突泉县|