首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用OpenXml在C#中从word文档中删除两个书签中的数据或表

如何使用OpenXml在C#中从word文档中删除两个书签中的数据或表
EN

Stack Overflow用户
提问于 2012-10-30 11:24:45
回答 1查看 2.6K关注 0票数 0

我正在C#中使用OpenXML创建word文档。我可以在指定的书签后面插入文本,但是如何在两个书签中删除数据。

下面是在指定书签之后插入文本的代码。

代码语言:javascript
复制
 string fileName = @"C:\Users\sharepointadmin\Desktop\volc.docx";

            TableValues ObjTableValues = new TableValues();
            List<TableValues> allValues = new System.Collections.Generic.List<TableValues>();

            for (int i = 1; i <= 5; i++)
            {
                ObjTableValues = new TableValues();
                ObjTableValues.EmpID = i.ToString();
                ObjTableValues.EmpName = "Emp" + i.ToString();
                ObjTableValues.EmpDesig = "SE";
                ObjTableValues.EmpDept = "Software";

                allValues.Add(ObjTableValues);
                //ConvertMailMergeEscape(fileName);

            }

            AddTable(fileName, allValues);

        }
            using (var document = WordprocessingDocument.Open(fileName, true))
            {
                IDictionary<String, BookmarkStart> bookmarkMap = new Dictionary<String, BookmarkStart>();               

                var doc = document.MainDocumentPart.Document;
                var mainpart = document.MainDocumentPart;
                var res = from ObjTableValues in mainpart.Document.Body.Descendants<BookmarkStart>() where ObjTableValues.Name == "testbookmark" select ObjTableValues;                
                var bookmark = res.SingleOrDefault();
                if (bookmark != null)
                {
                    var parent = bookmark.Parent;
 run.Append(text);
                    Paragraph newParagraph = new Paragraph(run);

                    // insert after bookmark parent
                    parent.InsertAfterSelf(newParagraph);
  foreach (BookmarkStart bookmarkStart in document.MainDocumentPart.RootElement.Descendants<BookmarkStart>())
                {
                    bookmarkMap[bookmarkStart.Name] = bookmarkStart;
                }

                MoveToRangeStart ranstart = new MoveToRangeStart();


                foreach (BookmarkStart bookmarkStart in bookmarkMap.Values)
                {
                    Run bookmarkText = bookmarkStart.NextSibling<Run>();
                    if (bookmarkText != null)
                    {
                        //bookmarkText.GetFirstChild<Text>().Text = "blah";
                    }
                }

                DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table();

                TableProperties props = new TableProperties(
                    new TableBorders(
                    new TopBorder
                    {
                        Val = new EnumValue<BorderValues>(BorderValues.Single),
                        Size = 12
                    },
                    new BottomBorder
                    {
                        Val = new EnumValue<BorderValues>(BorderValues.Single),
                        Size = 12
                    },
                    new LeftBorder
                    {
                        Val = new EnumValue<BorderValues>(BorderValues.Single),
                        Size = 12
                    },
                    new RightBorder
                    {
                        Val = new EnumValue<BorderValues>(BorderValues.Single),
                        Size = 12
                    },
                    new InsideHorizontalBorder
                    {
                        Val = new EnumValue<BorderValues>(BorderValues.Single),
                        Size = 12
                    },
                    new InsideVerticalBorder
                    {
                        Val = new EnumValue<BorderValues>(BorderValues.Single),
                        Size = 12
                    }));

                table.AppendChild<TableProperties>(props);

                foreach (TableValues Tableitem in allValues)
                {
                    var tr = new DocumentFormat.OpenXml.Wordprocessing.TableRow();

                    var tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();

                    tc.Append(new Paragraph(new Run(new Text(Tableitem.EmpID))));
                    tr.Append(tc);

                    tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
                    tc.Append(new Paragraph(new Run(new Text(Tableitem.EmpName))));
                    tr.Append(tc);

                    tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
                    tc.Append(new Paragraph(new Run(new Text(Tableitem.EmpDesig))));
                    tr.Append(tc);

                    tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
                    tc.Append(new Paragraph(new Run(new Text(Tableitem.EmpDept))));
                    tr.Append(tc);

                    table.Append(tr);

                }
                doc.Body.Append(table);
                doc.Save();
            }
        }
    }
}

有人能帮我吗。

EN

回答 1

Stack Overflow用户

发布于 2012-10-30 16:53:39

我假设您希望从word文档中删除一些表或数据。如果是这样,我建议您启用Microsoft中的developer选项卡。对于Microsoft 2007,如果单击Office并转到"Word选项“按钮,该按钮位于下拉菜单的底部。现在启用显示“开发人员选项卡在丝带”。一旦激活了developer选项卡,您就可以在Microsoft中看到一个附加的选项卡"Developer“。在此选项卡中,如果单击Rich图标(标记为Aa),将在word文档中插入一个标记。现在,如果您右键单击该标记,您可以给这个标记一个名称和id。

现在,您可以通过C#中的id或名称来访问该标记。你给出的标签名是“测试标签”

代码语言:javascript
复制
    MainDocumentPart mainPart = doc.MainDocumentPart;
    List<SdtBlock> taggedContentControls = mainPart.Document.Descendants<SdtBlock>().ToList();
    foreach (var tagControl in taggedContentControls)
                    {
                        string tagName = tagControl.Descendants<SdtAlias>().First().Val.Value;
                        if(tagName== "Test Tag")
                        {
// you can insert any data in this tag over here
}

现在,使用类似的方法,假设您想要删除的这个标记中有一个表和一些其他数据

代码语言:javascript
复制
foreach (var tagControl in taggedContentControls)
                {
                    string tagName = tagControl.Descendants<SdtAlias>().First().Val.Value;
                    if (tagName.Contains("Test Tag"))
                    {
// If we want to delete table only
                        //tagControl.SdtContentBlock.Descendants<Table>().First().Remov();       
// If you want to remove everything in this tag                        
tagControl.SdtContentBlock.Remove();
                    }
                }

不要忘记在操作结束时保存您的文档,我的意思是mainpart.Document.Save();

为了简单起见,我编写了多个LINQ语句,以便从文档中获取标记。你可以根据你的理解来改变它们。

我希望它能帮助你解决你的问题。

问候

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13137974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档