コード例 #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Inserts a table with 300 rows using Open Xml
            object bookmarkName = "bkflatOpc";

            if (!this.Bookmarks.Exists("bkflatOpc"))
            {
                MessageBox.Show("Please create a bookmark with name bkflatOpc");
                return;
            }
            Word.Bookmark tblBookmark = this.Bookmarks.get_Item(ref bookmarkName);
            if (tblBookmark.Range.Tables.Count > 0)
            {
                MessageBox.Show("Table already exists.Please delete this table.");
                return;
            }
            this.Application.ScreenUpdating = false;
            string openxml     = string.Empty;
            int    incrementor = 1;

            //Get existing content control id , if there is any
            if (this.ContentControls.Count > 0)
            {
                object index           = this.ContentControls.Count;
                Word.ContentControl cc = this.ContentControls.get_Item(ref index);
                contentControlSeedId = Int32.Parse(cc.ID);
                contentControlSeedId = contentControlSeedId + 1;
            }
            //Get stream for the range. This is the System.IO.Packaging.Package stream
            Stream packageStream = this.Paragraphs[1].Range.GetPackageStreamFromRange();

            //Use Open Xml SDK to process it.
            using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(packageStream, true))
            {
                //Insert image to Image Part
                ImagePart imgPart =
                    wordDoc.MainDocumentPart.AddImagePart(ImagePartType.Jpeg, "RImage");
                System.Drawing.Image img =
                    (System.Drawing.Image)Resources.ResourceManager.GetObject("RImage");
                img.Save(imgPart.GetStream(), System.Drawing.Imaging.ImageFormat.Jpeg);
                //Remove all children
                wordDoc.MainDocumentPart.Document.Body.RemoveAllChildren();
                //Generate table markup.
                DocumentFormat.OpenXml.Wordprocessing.Table table =
                    new DocumentFormat.OpenXml.Wordprocessing.Table();
                TableProperties properties =
                    new TableProperties(
                        new DocumentFormat.OpenXml.Wordprocessing.TableStyle()
                {
                    Val = "TableGrid"
                },
                        new TableWidth()
                {
                    Width = 0, Type = TableWidthUnitValues.Auto
                },
                        new TableLook()
                {
                    Val = "04A0"
                });
                table.AppendChild <TableProperties>(properties);
                for (int i = 0; i < 301; i++)
                {
                    TableRow row = new TableRow();
                    for (int j = 0; j < 3; j++)
                    {
                        incrementor++;
                        SdtCell contentControl =
                            GenerateSdtCell("Sample Text", incrementor + contentControlSeedId);
                        UInt32 value = (UInt32)(incrementor + contentControlSeedId);
                        contentControl
                        .Descendants <Run>()
                        .First()
                        .AppendChild <Drawing>
                            (GenerateDrawing("RImage", value, "RImage.jpg", 321933L, 288000L,
                                             19050L, 17957L, 0L, 0L));
                        row.AppendChild <SdtCell>(contentControl);
                    }
                    table.AppendChild <TableRow>(row);
                }
                int bookmarkCount = this.Bookmarks.Count;
                table.
                PrependChild <BookmarkStart>(new BookmarkStart()
                {
                    Name = "bkflatOpc",
                    Id   = bookmarkCount.ToString()
                }
                                             );
                table.
                AppendChild <BookmarkEnd>
                    (new BookmarkEnd()
                {
                    Id = bookmarkCount.ToString()
                });
                wordDoc.MainDocumentPart.Document
                .Body
                .AppendChild <DocumentFormat.OpenXml.Wordprocessing.Table>(table);
                wordDoc.MainDocumentPart.Document.Save();
                //Flush the contents of the package
                wordDoc.Package.Flush();
                //Convert back to flat opc using this in-memory package
                XDocument xDoc = OpcHelper.OpcToFlatOpc(wordDoc.Package);
                openxml = xDoc.ToString();
            }
            this.Application.ScreenUpdating = false;
            Word.Range range = FindRange("bkflatOpc");
            //Insert this flat opc Xml
            range.InsertXML(openxml, ref missing);
            this.Application.ScreenUpdating = true;
        }