'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. Linux文件的管理

    创建:touch  vim/vi  echo重定向  cat touch 管理: atime  mtime touch  文件名  //如果文件不存在,不创建文件 删除:mv /tmp find |x ...

  2. Mysql(九):Python连接MySQL数据库之pymysql模块使用

    Python3连接MySQL 本文介绍Python3连接MySQL的第三方库--PyMySQL的基本使用. PyMySQL介绍 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服 ...

  3. DA_03_linux网络配置及其远程连接

    一: 修改Linux的基本配置 直接运行:setup,根据提示修改 1.修改主机名:vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=shizhan1 ...

  4. css优化和重排

    转:http://caibaojian.com/css-reflow-repaint.html

  5. java中的集合总结

    知识点: 集合框架和List.set.Map相关集合特点的描述 Collection接口常用方法,List中相对Collection新增的方法,Collection的遍历(一般for循环,增强for循 ...

  6. vector和list插入性能对比

    int main() { clock_t t1 =clock(); vector<string> vec_Str; ;i<;i++) { vec_Str.push_back(&quo ...

  7. Vue项目中的文件导出

    项目中涉及到文件导出,分xml和excel导出.不同的文件导出格式不同,需要根据文件类型判断导出格式. exportAllData(val){ //全部导出 if(!val){ this.export ...

  8. struts2之登陆拦截

    针对登录拦截功能,我们需要设置拦截哪些方法和不拦截哪些方法 action action类中,处理登录时,将用户.密码绑定到session ActionContext ac = ActionContex ...

  9. [Cypress] Use the Most Robust Selector for Cypress Tests

    Which selectors your choose for your tests matter, a lot. In this lesson, we'll see the recommended ...

  10. docker国内镜像地址

    https://registry.docker-cn.com http://hub-mirror.c.163.com https://docker.mirrors.ustc.edu.cn