XML导出word

前端之家收集整理的这篇文章主要介绍了XML导出word前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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;
        }
原文链接:https://www.f2er.com/xml/299251.html

猜你在找的XML相关文章