程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 向SdtBlock中添加Table

向SdtBlock中添加Table

編輯:C#入門知識

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using DocumentFormat.OpenXml.Packaging; 
using DocumentFormat.OpenXml.Wordprocessing; 
using System.IO; 
 
namespace ConsoleApplication15 

    class Program 
    { 
        [STAThread] 
        static void Main(string[] args) 
        { 
            OpenFileDialog ofd = new OpenFileDialog(); 
            ofd.Multiselect = false; 
            ofd.Filter = "Word Document|*.docx"; 
            ofd.ShowDialog(); 
            string usepath =  AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "test.docx"; 
            File.Copy(ofd.FileName,usepath,true); 
            using (WordprocessingDocument wpd = WordprocessingDocument.Open(usepath, true)) 
            { 
                MainDocumentPart mdp = wpd.MainDocumentPart; 
                Document dc = mdp.Document; 
                SdtBlock target = null; 
                foreach (SdtBlock sb in dc.Descendants<SdtBlock>()) 
                { 
                    Tag tg = sb.Descendants<Tag>().Where(T => T.Val == "TestField").FirstOrDefault(); 
                    if (tg != null) 
                    { 
                        target = sb; 
                        break; 
                    } 
                } 
                if (target != null) 
                { 
                    SdtContentBlock scb = target.SdtContentBlock; 
                    Table tb = new Table(); 
                    TableGrid tg = new TableGrid(); 
                    GridColumn gc = new GridColumn() { Width = "2840" }; 
                    GridColumn gc1 = new GridColumn() { Width = "2840" }; 
                    tg.Append(gc); 
                    tg.Append(gc1); 
                    tb.Append(tg); 
                    TableRow tr = new TableRow(); 
                    TableCell tc = new TableCell(); 
                    Paragraph p = new Paragraph(); 
                    TableCell tc1 = new TableCell(); 
                    Paragraph p1 = new Paragraph(); 
                    tc.Append(p); 
                    tc1.Append(p1); 
                    tr.Append(tc); 
                    tr.Append(tc1); 
                    tb.Append(tr); 
                    scb.Append(tb); 
                } 
                dc.Save(); 
            } 
        } 
    } 

 


摘自 TX_OfficeDev的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved