none
Need help for OpenXML inserting a table randomly in the stdblock RRS feed

  • Question

  • Hi  Team i am using open xml.

    I am trying to append the a table inside a content control.

    For this first i created a table and when i am trying to append.

    DocumentFormat.OpenXml.Packaging.MainDocumentPart mainPart= file.MainDocumentPart;SdtBlock cc = mainPart.Document.Body.Descendants<SdtBlock>().Where(r => r.SdtProperties.GetFirstChild<Tag>().Val == "TagName").Single();

    -- Create table logic

    -- Table name is theTable

    cc.Append(theTable)

    please let me know where i am going wrong.

    Thursday, May 17, 2012 7:10 AM

Answers

  • Hi NishanSharma04,

    Thanks for posting in the MSDN Forum.

    This snippet will show you a simply sample. This table will no have border line. If you want to draw border line please insert style into style part by yourself.

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

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Friday, May 18, 2012 5:17 AM
    Moderator

All replies

  • Any help would be much appreciated.
    Thursday, May 17, 2012 9:46 AM
  • Hi NishanSharma04,

    Thanks for posting in the MSDN Forum.

    This snippet will show you a simply sample. This table will no have border line. If you want to draw border line please insert style into style part by yourself.

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

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Friday, May 18, 2012 5:17 AM
    Moderator