0

OpenXML を使用して C# で Word 文書を作成しています。指定したブックマークの後にテキストを挿入できますが、2 つのブックマーク内のデータを削除するにはどうすればよいですか。

以下は、指定されたブックマークの後にテキストを挿入するコードです。

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

誰でも私を助けてください。

4

1 に答える 1

1

Word文書からテーブルまたはデータを削除したいと思います。その場合は、Microsoft Word で開発者タブを有効にすることをお勧めします。Microsoft Word 2007 の場合、[Office ボタン] をクリックして、ドロップダウン メニューの下部にある [Word のオプション] ボタンに移動します。ここで、[リボンに [開発] タブを表示する] を有効にします。開発者タブを有効にすると、Microsoft Word に追加のタブ「開発者」が表示されます。このタブからリッチ テキスト アイコン ( Aaでマーク) をクリックすると、Word 文書にタグが挿入されます。タグを右クリックすると、このタグに名前と ID を付けることができます。

これで、C# の ID または名前でこのタグにアクセスできるようになりました。たとえば、指定したタグ名は「Test Tag」です

    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
}

同様のアプローチで、このタグに削除したいテーブルとその他のデータがあるとします。

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 ステートメントを作成しました。あなたの理解に応じてそれらを変更することができます。

問題を整理するのに役立つことを願っています。

よろしく

于 2012-10-30T16:53:39.537 に答える