Replace bookmark text in Word file using Open XML SDK

前端 未结 11 2040
眼角桃花
眼角桃花 2020-12-01 08:24

I assume v2.0 is better... they have some nice \"how to:...\" examples but bookmarks don\'t seem to act as obviously as say a Table... a bookmark is defined by two

11条回答
  •  难免孤独
    2020-12-01 08:49

    I needed to replace the text of a bookmark (bookmarks name is "Table") with a table. This is my approach:

    public void ReplaceBookmark( DatasetToTable( ds ) )
    {
        MainDocumentPart mainPart = myDoc.MainDocumentPart;
        Body body = mainPart.Document.GetFirstChild();
        var bookmark = body.Descendants()
                            .Where( o => o.Name == "Table" )
                            .FirstOrDefault();
        var parent = bookmark.Parent; //bookmark's parent element
        if (ds!=null)
        {
            parent.InsertAfterSelf( DatasetToTable( ds ) );
            parent.Remove();
        }
        mainPart.Document.Save();
    }
    
    
    public Table DatasetToTable( DataSet ds )
    {
        Table table = new Table();
        //creating table;
        return table;
    }
    

    Hope this helps

提交回复
热议问题