0

我正在将数据库中的数据输入到 MS Word 中的书签中,每个数据都输入正确,但问题是数据的位置是输入的 - 对于每个相关的书签 - 在行尾而不是我添加书签的位置,并且我不知道为什么会这样!我在 C# 上使用 opemXml

更新

    //  Get the main document part (document.xml).
    foreach (System.IO.Packaging.PackageRelationship documentRelationship in package.GetRelationshipsByType(documentRelationshipType))
    {
        NameTable nt = new NameTable();
        nsManager = new XmlNamespaceManager(nt);
        nsManager.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");

    Uri documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), documentRelationship.TargetUri);
    documentPart = package.GetPart(documentUri);

    #region Update Document Bookmarks
    //Get document xml
    XmlDocument xdoc = new XmlDocument();
    //xdoc.Load(documentPart.GetStream());
    xdoc.Load(documentPart.GetStream(FileMode.Open, FileAccess.Read));

    //Select all bookmark nodes
    XmlNodeList nodeList = xdoc.SelectNodes("//w:bookmarkStart", nsManager);
    foreach (XmlNode node in nodeList)
    {
        if (this.SetBookmarkText(xdoc, node, "DepositNO", GetDepositNO(DepositNo))) continue;
        if (this.SetBookmarkText(xdoc, node, "AppDate", FormFunctions.Get_Application_Date(DepositNo).ToShortDateString())) continue;
        if (this.SetBookmarkText(xdoc, node, "Title", FormFunctions.Get_Application_Title(DepositNo, 0))) continue;
        if (this.SetBookmarkText(xdoc, node, "ApplicantName", Get_Application_CV_Name(AppID, 6))) continue;
        if (this.SetBookmarkText(xdoc, node, "AgentName", Get_Application_CV_Name(AppID, 7))) continue;
        if (this.SetBookmarkText(xdoc, node, "Address", Address)) continue;
        if (this.SetBookmarkText(xdoc, node, "AgentName01", Get_Application_CV_Name(AppID, 7))) continue;
        if (this.SetBookmarkText(xdoc, node, "Times", Times)) continue;
        if (this.SetBookmarkText(xdoc, node, "A", A)) continue;
        if (this.SetBookmarkText(xdoc, node, "B", B)) continue;
        if (this.SetBookmarkText(xdoc, node, "Approve", Approve)) continue;
        if (this.SetBookmarkText(xdoc, node, "RequestArabic", RequestArabic)) continue;
        if (this.SetBookmarkText(xdoc, node, "ExamTime", ExamTime)) continue;
        if (this.SetBookmarkText(xdoc, node, "Mark", Mark)) continue;
        if (this.SetBookmarkText(xdoc, node, "Fees", Fees)) continue;
        if (this.SetBookmarkText(xdoc, node, "Objection", Objection)) continue;
        if (this.SetBookmarkText(xdoc, node, "Rejection", Rejection)) continue;
        if (this.SetBookmarkText(xdoc, node, "ExamTime01", ExamTime)) continue;
        if (this.SetBookmarkText(xdoc, node, "DepositNO01", GetDepositNO(DepositNo))) continue;
    }
    #endregion

    StreamWriter streamPart = new StreamWriter(documentPart.GetStream(FileMode.Open, FileAccess.Write));
    xdoc.Save(streamPart);
    streamPart.Close();
}

package.Flush();
package.Close();

//send response to browser
string File_Name = destenation + docFileName;

//string popupScript = "<script language='javascript'>" + "window.open('" + File_Name + "', 'Document', " + "'width=700, height=600, menubar=yes, resizable=yes')" + "</script>";
//ClientScript.RegisterClientScriptBlock(this.GetType(), "PopupScriptOffer", popupScript);

Response.AddHeader("Content-Disposition", "attachment; filename=" + docFileName);
Response.WriteFile(File_Name);
Response.End();

SetBookmarkText 函数

private bool SetBookmarkText(XmlDocument xdoc, XmlNode node, string bookmarkName, string bookmarkValue)
    {
        if (node.NextSibling.Name.ToString() == "w:bookmarkEnd")
        {
            if (node.Attributes["w:name"].Value == bookmarkName)
            {
                //get the node previous sibling style ("w:rPr") to apply to the bookmark text
                XmlNode nodeStyle = node.PreviousSibling.CloneNode(true);

            //parent node "w:p"
            XmlNode bookmrkParent = node.ParentNode;

            XmlElement tagRun;
            tagRun = xdoc.CreateElement("w:r", nsManager.LookupNamespace("w"));
            bookmrkParent.AppendChild(tagRun);

            if (nodeStyle.SelectSingleNode("//w:rPr", nsManager) != null)
                tagRun.AppendChild(nodeStyle.SelectSingleNode("//w:rPr", nsManager));

            XmlElement tagText;
            tagText = xdoc.CreateElement("w:t", nsManager.LookupNamespace("w"));
            tagRun.AppendChild(tagText);

            //*** insert text into part as a Text node 
            XmlNode nodeText;
            nodeText = xdoc.CreateNode(XmlNodeType.Text, "w:t", nsManager.LookupNamespace("w"));
            nodeText.Value = bookmarkValue;
            tagText.AppendChild(nodeText);

            return true;
        }
    }
    return false;
}
4

1 回答 1

0

书签的替换节点已添加到父节点的末尾。

换行

bookmrkParent.AppendChild(tagRun);

bookmrkParent.InsertAfter(tagRun, node);

此代码还需要您的书签带有空文本。即,书签标签之后的下一个节点始终应该是bookmarkEnd。如果您有一些示例文本作为书签内容,它将不起作用。

请像这样替换顶部的几行

// if (node.NextSibling.Name.ToString() == "w:bookmarkEnd")
        {
            if (node.Attributes["w:name"].Value == bookmarkName)
            {
                    while (!node.NextSibling.Name.ToString().Equals("w:bookmarkEnd"))
                    {
                        node.ParentNode.RemoveChild(node.NextSibling);
                    }
......

如果我遇到更多问题,将更新。

当书签遍历单个父节点时,您的逻辑会出现问题。我还在解决它。

于 2013-02-07T10:05:33.483 回答