Add Table to SdtBlock

Source: Internet
Author: User

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

 


From TX_OfficeDev's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.