VBA读写XML文件
'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文件的更多相关文章
- C#读写xml文件的常用方法
已知有一个XML文件(bookshop.xml)如下: <?xml version="1.0" encoding="gb2312" ?> <b ...
- PHP读写XML文件的四种方法
PHP对XML文件进行读写操作的方法一共有四种,分别是:字符串方式直接读写.DOMDocument读写. XMLWrite写和XMLReader读.SimpleXML读写,本文将依次对这四种方法进行介 ...
- Java 读写XML文件 API--org.dom4j
om4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个十分优秀的JavaXML API,具有性能优异.功能强大和极其易使用的特点,同时它也是一个开放源代码的软件 ...
- Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件
Fixflow引擎解析(四)(模型) - 通过EMF扩展BPMN2.0元素 Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件 Fixflow引擎解析(二)(模型) - BPMN ...
- cocos2d-x 读写 xml 文件
cocos2d-x 读写 xml 文件 A product of cheungmine使用cocos2d-x开发2d游戏确实方便,但是对于一般的小游戏,经常需要的工作是UI布局设计和调整,代码改来改去 ...
- java通过dom读写xml文件
java通过dom读写xml文件 要读的xml文件 <?xml version="1.0" encoding="GB2312"?><学生花名册 ...
- C# 读写XML文件的方法
C# 读写XML文件的方法 一.写XML文件 XmlDocument xmlDocument = new XmlDocument();xmlDocument.AppendChild(xmlDocume ...
- VC 使用msxml6.dll动态链接库中的函数读写XML文件
VC 使用msxml6.dll动态链接库中的函数读写XML文件 目录 1 引言 2 .dll使用方法 3 常用函数总结 4 实例应用 5 运行效果预览 6 补充说明 7 不足之处 8 更新 引言: ...
- 【VS开发】【计算机视觉】OpenCV读写xml文件《C++版本》
OpenCV FileStorage类读写XML/YML文件 在OpenCV程序中,需要保存中间结果的时候常常会使用.xml / .yml文件,opencv2.0之前都是使用C风格的代码,当时读写XM ...
随机推荐
- SURF算法源代码OPENSURF分析
SURF算法源代码分析 平台:win x64 + VS2015专业版 +opencv2.4.11 配置类似参考OPENSIFT,参考我的另一篇博客:https://www.cnblogs.com/Al ...
- Async 配置线程池
需要注意一下 ThreadPoolTaskExecutor 和 Executor 区别 @Configuration public class ExecutorConfig { /** Set t ...
- kubernetes之健康状态检测
1.说明 容器探针: kubelet 对容器执行的定期诊断 探针执行方式: LivenessProbe: 判断容器是否存活 running状态, 如果不健康kubelet就会杀掉pod,根据重启策略R ...
- 自动化测试环境搭建(appium+selenium+python)
一.需要安装的软件(根据你所需要的版本安装即可,不一定必须按照小编的版本来) JDK:1.8.0_171 Python:3.7.1 node.js:10.13.0 android-sdk_r24.4. ...
- Python 去除字符串中的空行
Python 去除字符串中的空行 mystr = 'adfa\n\n\ndsfsf' print("".join([s for s in mystr.splitlines(True ...
- table 随着内容自动适应宽度
td { white-space: nowrap; } 给td加个属性就可以了,如果有th则可以 td,th 本文来自:https://blog.csdn.net/liuhongwei_study/a ...
- (五)zabbix微信报警
1.注册微信企业号 1)注册微信企业号 https://work.weixin.qq.com 2)通讯录添加用户 3)记住部门id 4)创建应用 5)点击刚创建的应用,记住Agentld和secret ...
- HTTP 协议讲解
http请求由三部分组成,分别是:请求行.消息报头.请求正文 HTTP(超文本传输协议)是一个基于请求与响应模式的.无状态的.应用层的协议,常基于TCP的连接方式,HTTP1.1版本中给出一种持续连接 ...
- vue中前进刷新、后退缓存用户浏览数据和浏览位置的实践
vue中前进刷新.后退缓存用户浏览数据和浏览位置的实践 2018年07月07日 11:58:40 大灰狼的小绵羊哥哥 阅读数:4492 vue中,我们所要实现的一个场景就是: 1.搜索页面==&g ...
- The Reset Method of Te Philips VTR 5210
Pull down and hold the ON/OFF buttun, Then press the play button