加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

XML导出word

发布时间:2020-12-16 05:08:29 所属栏目:百科 来源:网络整理
导读:XML导出word类 新建一个word文档,如图 在相应单元格中插入书签 将word文档另存为xml文档 后台代码: ExportWordML expwordml = new ExportWordML(); expwordml.CreateXmlDom(Server.MapPath("~/Template/人员档案表.xml")); Dictionarystring,string wordTex

XML导出word类

新建一个word文档,如图


在相应单元格中插入书签

将word文档另存为xml文档

后台代码:

            ExportWordML expwordml = new ExportWordML();
            expwordml.CreateXmlDom(Server.MapPath("~/Template/人员档案表.xml"));
            Dictionary<string,string> wordTexts = new Dictionary<string,string>();
            Dictionary<string,Utility.ExportWordML.WordPic> dic_wordpic = new Dictionary<string,ExportWordML.WordPic>();
	    wordTexts.Add("user_name" + i,"张三");
            wordTexts.Add("sex" + i,"男");
	    expwordml.SetNodeText(wordTexts);
            Utility.ExportWordML.WordPic portrait_pic = new ExportWordML.WordPic();
            portrait_pic.Width = 85;
            portrait_pic.Height = 90;
            portrait_pic.PicPath = Server.MapPath(picturePath);
            dic_wordpic.Add("Portrait_url",portrait_pic);
	    expwordml.SetNodePic(dic_wordpic);
	    expwordml.Save(Server.MapPath("~/Template/EntryfileTemp/员工档案.doc"); 


插入图片后导出的文档使用word打开时,报了格式错误,于是对类中的插入图片方法做了下修改

        /// <summary>
        /// 根据标签插入一张图片
        /// </summary>
        /// <param name="xdoc"></param>
        /// <param name="node"></param>
        /// <param name="picpaths"></param>
        private void SetBookMarkPics(XmlDocument xdoc,XmlNode node,Dictionary<string,WordPic> picpaths)
        {
            if (node.NextSibling.Name.ToString() == "aml:annotation")//w:bookmarkEnd
            {
                //查找bookmarks中是否在word标签
                if (picpaths.ContainsKey(node.Attributes["w:name"].Value))
                {
                    // "w:p"节点的父节点
                    XmlNode bookmrkParent = node.ParentNode;
                    XmlNode bookmrkParentParent = node.ParentNode.ParentNode;

                    XmlElement tagPic;
                    tagPic = xdoc.CreateElement("w:pict",nsmgr.LookupNamespace("w"));
                    bookmrkParent.InsertAfter(tagPic,node);//原为 bookmrkParentParent.InsertAfter(tagPic,bookmrkParent);

                    XmlElement tagbindData;
                    tagbindData = xdoc.CreateElement("w:binData",nsmgr.LookupNamespace("w"));
                    tagPic.AppendChild(tagbindData);

                    XmlAttribute nodeAttr;
                    nodeAttr = xdoc.CreateAttribute("w:name",nsmgr.LookupNamespace("w"));
                    nodeAttr.Value = "wordml://" + Path.GetFileName(picpaths[node.Attributes["w:name"].Value].PicPath);
                    tagbindData.Attributes.Append(nodeAttr);

                    //xml:space="preserve"
                    nodeAttr = xdoc.CreateAttribute("xml:space");
                    nodeAttr.Value = "preserve";
                    tagbindData.Attributes.Append(nodeAttr);


                    XmlNode nodePicValue;
                    nodePicValue = xdoc.CreateNode(XmlNodeType.Text,"w:binData",nsmgr.LookupNamespace("w"));
                    nodePicValue.Value = ImgToBase64String(picpaths[node.Attributes["w:name"].Value].PicPath);
                    tagbindData.AppendChild(nodePicValue);

                    XmlElement tagShape;
                    tagShape = xdoc.CreateElement("v:shape",nsmgr.LookupNamespace("v"));
                    tagPic.AppendChild(tagShape);


                    XmlAttribute tagStyle;
                    tagStyle = xdoc.CreateAttribute("style");
                    tagStyle.Value = "width:" + picpaths[node.Attributes["w:name"].Value].Width + "pt;height:" + picpaths[node.Attributes["w:name"].Value].Height + "pt;";
                    tagShape.Attributes.Append(tagStyle);


                    XmlElement tagimagedata;
                    tagimagedata = xdoc.CreateElement("v:imagedata",nsmgr.LookupNamespace("v"));
                    tagShape.AppendChild(tagimagedata);

                    nodeAttr = xdoc.CreateAttribute("src");
                    nodeAttr.Value = "wordml://" + Path.GetFileName(picpaths[node.Attributes["w:name"].Value].PicPath);
                    tagimagedata.Attributes.Append(nodeAttr);

                }
            }
        }



另附合并xml文档的方法
        /// <summary>
        /// 合并XML文档,将strCopyFolder中的XML文档合并到path1中
        /// </summary>
        public string MergeXML(string path1,string strCopyFolder,string savepath)
        {
            XmlDocument doc1 = new XmlDocument();
            XmlDocument doc2 = new XmlDocument();
            doc1.Load(path1);
            nsmgr = new XmlNamespaceManager(doc1.NameTable);
            nsmgr.AddNamespace("w","http://schemas.microsoft.com/office/word/2003/wordml");
            XmlNodeList nodeList1 = doc1.SelectNodes("//w:body",nsmgr);
            XmlNode node1_body;

            string[] arrFiles = Directory.GetFiles(strCopyFolder);

            node1_body = nodeList1.Item(0);
            foreach (string strFile in arrFiles)
            {
                doc2.Load(strFile);
                foreach (XmlNode n in doc2.SelectSingleNode("//w:body",nsmgr).ChildNodes)
                {
                    XmlNode node2 = doc1.ImportNode(n,true);
                    node1_body.AppendChild(node2);
                }
            }
            doc1.Save(savepath);
            return savepath;
        }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读