'Write XML file
Sub WriteXML(fpa$, fn$)
Dim xmlfile As String
xmlfile = ThisWorkbook.Path & ".\Export.xml"
CreateXml xmlfile, fpa, fn
End Sub Function CreateXml(xmlfile$, fpa$, fn$)
Dim xdoc As Object
Dim rootNode As Object
Dim header As Object
Dim newNode As Object
Dim tNode As Object Set xdoc = CreateObject("MSXML2.DOMDocument")
Set rootNode = xdoc.createElement("FilePath")
Set xdoc.DocumentElement = rootNode
'xDoc.Load xmlFile
Set header = xdoc.createProcessingInstruction("xml", "version='1.0' encoding='Unicode'")
xdoc.InsertBefore header, xdoc.ChildNodes() Set newNode = xdoc.createElement("File")
Set tNode = xdoc.DocumentElement.appendChild(newNode)
tNode.setAttribute "type", "folder" Set newNode = xdoc.createElement("path")
Set tNode = xdoc.DocumentElement.ChildNodes.Item().appendChild(newNode)
tNode.appendChild (xdoc.createTextNode(fpa)) Set newNode = xdoc.createElement("File")
Set tNode = xdoc.DocumentElement.appendChild(newNode)
tNode.setAttribute "type", "file" Set newNode = xdoc.createElement("name")
Set tNode = xdoc.DocumentElement.ChildNodes.Item().appendChild(newNode)
tNode.appendChild (xdoc.createTextNode(fn)) Set newNode = Nothing
Set tNode = Nothing Dim xmlStr As String
xmlStr = PrettyPrintXml(xdoc)
WriteUtf8WithoutBom xmlfile, xmlStr Set rootNode = Nothing
Set xdoc = Nothing 'MsgBox xmlFile & "XML file exported sucessfully!"
' Call export_data(fpa, fn)
End Function 'Formatting XML,set wrapping and indentation
Function PrettyPrintXml(xmldoc) As String
Dim reader As Object
Dim writer As Object
Set reader = CreateObject("Msxml2.SAXXMLReader.6.0")
Set writer = CreateObject("Msxml2.MXXMLWriter.6.0")
writer.indent = True
writer.omitXMLDeclaration = True
reader.contentHandler = writer
reader.Parse (xmldoc)
PrettyPrintXml = writer.Output
End Function 'UTF-8 without BOM
Function WriteUtf8WithoutBom(filename As String, content As String)
Dim stream As New ADODB.stream
stream.Open
stream.Type = adTypeText
stream.Charset = "utf-8"
stream.WriteText "<?xml version=" & Chr() & "1.0" & Chr() & _
" encoding=" & Chr() & "UTF-8" & Chr() & "?>" & vbCrLf
stream.WriteText content 'Top 3 character move sets£¨0xEF,0xBB,0xBF£©
stream.Position = Dim newStream As New ADODB.stream
newStream.Type = adTypeBinary
newStream.Mode = adModeReadWrite
newStream.Open stream.CopyTo newStream
stream.Flush
stream.Close newStream.SaveToFile filename, adSaveCreateOverWrite
newStream.Flush
newStream.Close
End Function Sub export_data()
Dim xdoc As New DOMDocument60 'Declare and create XML object
Dim b As Boolean, root As IXMLDOMElement
Dim fp As String
Dim fn As String
Dim wb As Workbook
Dim arr() As String
Dim i As Integer
Dim j As Integer
Dim app As Object
Dim wbs As Workbook
Dim ws As Worksheet
Dim irow As Integer
On Error Resume Next
With ThisWorkbook.Sheets()
b = xdoc.Load(ThisWorkbook.Path & ".\Export.xml")
If b = True Then
Set root = xdoc.DocumentElement 'Get the root node
fn = root.ChildNodes.Item().Text
fp = root.ChildNodes.Item().Text & fn & "-" & Format(Now(), "yyyymmdd") & ".xlsx"
irow = ThisWorkbook.Sheets().Range("a1000000").End(xlUp).Row
ActiveWorkbook.Sheets().Copy
ActiveWorkbook.SaveAs filename:=fp
irow = .Range("A1000000").End(xlUp).Row
.Range("A2:E" & irow).ClearContents
Else
MsgBox "Error:failed to load xml file!",
End If
End With
End Sub

VBA读写XML文件的更多相关文章

  1. C#读写xml文件的常用方法

    已知有一个XML文件(bookshop.xml)如下: <?xml version="1.0" encoding="gb2312" ?> <b ...

  2. PHP读写XML文件的四种方法

    PHP对XML文件进行读写操作的方法一共有四种,分别是:字符串方式直接读写.DOMDocument读写. XMLWrite写和XMLReader读.SimpleXML读写,本文将依次对这四种方法进行介 ...

  3. Java 读写XML文件 API--org.dom4j

    om4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个十分优秀的JavaXML API,具有性能优异.功能强大和极其易使用的特点,同时它也是一个开放源代码的软件 ...

  4. Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件

    Fixflow引擎解析(四)(模型) - 通过EMF扩展BPMN2.0元素 Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件 Fixflow引擎解析(二)(模型) - BPMN ...

  5. cocos2d-x 读写 xml 文件

    cocos2d-x 读写 xml 文件 A product of cheungmine使用cocos2d-x开发2d游戏确实方便,但是对于一般的小游戏,经常需要的工作是UI布局设计和调整,代码改来改去 ...

  6. java通过dom读写xml文件

    java通过dom读写xml文件 要读的xml文件 <?xml version="1.0" encoding="GB2312"?><学生花名册 ...

  7. C# 读写XML文件的方法

    C# 读写XML文件的方法 一.写XML文件 XmlDocument xmlDocument = new XmlDocument();xmlDocument.AppendChild(xmlDocume ...

  8. VC 使用msxml6.dll动态链接库中的函数读写XML文件

    VC 使用msxml6.dll动态链接库中的函数读写XML文件 目录 1 引言 2 .dll使用方法 3 常用函数总结 4 实例应用 5 运行效果预览 6 补充说明 7 不足之处 8 更新   引言: ...

  9. 【VS开发】【计算机视觉】OpenCV读写xml文件《C++版本》

    OpenCV FileStorage类读写XML/YML文件 在OpenCV程序中,需要保存中间结果的时候常常会使用.xml / .yml文件,opencv2.0之前都是使用C风格的代码,当时读写XM ...

随机推荐

  1. Comparing Sentence Similarity Methods

    Reference:Comparing Sentence Similarity Methods,知乎.

  2. thymeleaf小知识

    1.根据不同性别,显明不同的默认图片:th:if th:src   图片路径 <img th:if="${gender=='男'}" id="admission_p ...

  3. Spring 注解无效 空指针异常

    对于Java开发,现在Spring已成为一种生态,使用Spring成为一种享受,Spring的使用让开发变得更加便捷. 而Spring好用归好用,若不清楚里面的工作原理,使用的时候难免会踩到一些坑. ...

  4. 用JS将毫秒转化成天时分秒的时间格式

    function formatDuring(mss) { var days = parseInt(mss / (1000 * 60 * 60 * 24)); var hours = parseInt( ...

  5. flask调试模式

    想要启用调试模式,发现安装目前网上流行的两种方式均无法在Pycharm中打开调试模式. 1)直接在对象上设置 flask 更新到1.0以后 不支持使用debug =True 来开启调试模式了. Pri ...

  6. SSL虚拟主机

    1.生成公钥与私钥 [root@proxy ~]# cd /usr/local/nginx/conf [root@proxy ~]# openssl genrsa > cert.key //生成 ...

  7. Can't load Microsoft.ReportViewer.ProcessingObjectModel.dll

    本机的时候是能正常看到report,但deploy到别的机器上却不行,按说从本机拷个dll过去就可以,但怎么也找不到. 原来要在cmd那里输入C:\WINDOWS\assembly\GAC_MSIL ...

  8. HDU 1512 Monkey King(左偏堆)

    爱争吵的猴子 ★★☆ 输入文件:monkeyk.in 输出文件:monkeyk.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在一个森林里,住着N只好斗的猴子.开始,他们各 ...

  9. Ubuntu 出现 Invalid operation update 或 Invalid operation upgrade的解决办法

    输入 sudo apt update && sudo apt full-upgrade

  10. nginx变量与实列

    nginx内置变量 内置变量存放在  ngx_http_core_module 模块中,变量的命名方式和apache 服务器变量是一致的.总而言之,这些变量代表着客户端请求头的内容,例如$http_u ...