用VBA读取Excel表格输出到格式化的xml文件中
最近需要做一个一劳永逸的XML文档生成,给项目内部专用的,直接VBA方便了,才第一次用。现学现卖了。。。。抽时间还是系统的学习下这方面的知识吧
输出到UTF-8编码的XML文档。并且换行符是Unix的\n换行符。
Sub WriteToXml() Dim FilePath As String
Dim ClientID As String
Dim Name As String
Dim LastCol As Long
Dim LastRow As Long Dim fso As FileSystemObject
Set fso = New FileSystemObject Dim fst As Object
Set fst = CreateObject("ADODB.Stream") Dim stream As TextStream LastCol = ActiveSheet.UsedRange.Columns.Count
LastRow = ActiveSheet.UsedRange.Rows.Count ' Create a TextStream. ' Set stream = fso.OpenTextFile("D:\ClientConfig.xml", ForWriting, True) fst.Type = 'Specify stream type - we want To save text/string data.
fst.Charset = "utf-8" 'Specify charset For the source text data.
fst.Open 'Open the stream And write binary data To the object 'stream.WriteLine "<?xml version=" & Chr(34) & "1.0" & Chr(34) & " encoding=" & Chr(34) & "utf-8" & Chr(34) & "?>"
'stream.WriteLine "<config>"
'stream.WriteLine " <clients>" fst.WriteText "<?xml version=" & Chr() & "1.0" & Chr() & " encoding=" & Chr() & "utf-8" & Chr() & "?>" & Chr()
fst.WriteText "<config>" & Chr()
fst.WriteText " <clients>" & Chr() CellData = "" For Row = To LastRow ClientID = Cells(Row, ).Value
Name = Cells(Row, ).Value ' stream.WriteLine " <client clientid=" & Chr(34) & ClientID & Chr(34) & " name=" & Chr(34) & Name & Chr(34) & _
' " ip=" & Chr(34) & Chr(34) & " username=" & Chr(34) & "username" & Chr(34) & " password=" & Chr(34) & "password" & Chr(34) & _
' " upload=" & Chr(34) & "yes" & Chr(34) & " cachedvalidtime=" & Chr(34) & "172800" & Chr(34) & ">" 'stream.WriteLine " <grid savepath=" & Chr(34) & "/data/lwfd/client/{CLIENTID}/{TYPE}/{YYYYMMDD}" & Chr(34) & _
'" filename=" & Chr(34) & "{TYPE}_{CCC}_{YYYYMMDDHH}_{FFF}_{TT}.grib2" & Chr(34) & " >" & "</grid>" 'stream.WriteLine " </client>" fst.WriteText " <client clientid=" & Chr() & ClientID & Chr() & " name=" & Chr() & Name & Chr() & _
" ip=" & Chr() & Chr() & " username=" & Chr() & "username" & Chr() & " password=" & Chr() & "password" & Chr() & _
" upload=" & Chr() & "yes" & Chr() & " cachedvalidtime=" & Chr() & "" & Chr() & ">" & Chr() fst.WriteText " <grid savepath=" & Chr() & "/data/lwfd/client/{CLIENTID}/{TYPE}/{YYYYMMDD}" & Chr() & _
" filename=" & Chr() & "{TYPE}_{CCC}_{YYYYMMDDHH}_{FFF}_{TT}.grib2" & Chr() & " >" & "</grid>" & Chr() fst.WriteText " </client>" & Chr() Next Row ' stream.WriteLine " </clients>"
' stream.WriteLine "</config>"
' stream.Close fst.WriteText " </clients>" & Chr()
fst.WriteText "</config>" & Chr() fst.SaveToFile "D:\ClientConfig.xml", 'Save binary data To disk
MsgBox ("Job Done")
End Sub
以下是一个根据需求的代码调整:
Sub Process()
Dim FilePath As String
Dim ClientID As String
Dim Name As String
Dim LastCol As Long
Dim LastRow As Long Dim IDPreffix As String Dim fst As Object
Set fst = CreateObject("ADODB.Stream") Dim oldIDPreffix As String
Dim oldName As String LastCol = ActiveSheet.UsedRange.Columns.Count
LastRow = ActiveSheet.UsedRange.Rows.Count For Row = To LastRow
ClientID = Cells(Row, ).Value
Name = Cells(Row, ).Value If Row = Then
oldIDPreffix = Mid(ClientID, , )
oldName = Name
'打开流
fst.Type = 'Specify stream type - we want To save text/string data.
fst.Charset = "utf-8" 'Specify charset For the source text data.
fst.Open 'Open the stream And write binary data To the object fst.WriteText "<?xml version=" & Chr() & "1.0" & Chr() & " encoding=" & Chr() & "utf-8" & Chr() & "?>" & Chr()
fst.WriteText "<config>" & Chr()
fst.WriteText " <clients>" & Chr()
End If IDPreffix = Mid(ClientID, , ) If IDPreffix = oldIDPreffix Then 'write file
fst.WriteText " <client clientid=" & Chr() & ClientID & Chr() & " name=" & Chr() & Name & Chr() & _
" ip=" & Chr() & Chr() & " username=" & Chr() & "username" & Chr() & " password=" & Chr() & "password" & Chr() & _
" upload=" & Chr() & "yes" & Chr() & " cachedvalidtime=" & Chr() & "" & Chr() & ">" & Chr() fst.WriteText " <grid savepath=" & Chr() & "/data/lwfd/client/{CLIENTID}/{TYPE}/{YYYYMMDD}" & Chr() & _
" filename=" & Chr() & "{TYPE}_{CCC}_{YYYYMMDDHH}_{FFF}_{TT}.grib2" & Chr() & " >" & "</grid>" & Chr() fst.WriteText " </client>" & Chr() Else 'write file tail
fst.WriteText " </clients>" & Chr()
fst.WriteText "</config>" & Chr() 'save to file
fst.SaveToFile "D:\" & oldName & "_ClientConfig" & ".xml", 'Save binary data To disk
fst.flush
fst.Close oldIDPreffix = IDPreffix
oldName = Name '打开流
fst.Type = 'Specify stream type - we want To save text/string data.
fst.Charset = "utf-8" 'Specify charset For the source text data.
fst.Open 'Open the stream And write binary data To the object 'write file head
fst.WriteText "<?xml version=" & Chr() & "1.0" & Chr() & " encoding=" & Chr() & "utf-8" & Chr() & "?>" & Chr()
fst.WriteText "<config>" & Chr()
fst.WriteText " <clients>" & Chr() fst.WriteText " <client clientid=" & Chr() & ClientID & Chr() & " name=" & Chr() & Name & Chr() & _
" ip=" & Chr() & Chr() & " username=" & Chr() & "username" & Chr() & " password=" & Chr() & "password" & Chr() & _
" upload=" & Chr() & "yes" & Chr() & " cachedvalidtime=" & Chr() & "" & Chr() & ">" & Chr() fst.WriteText " <grid savepath=" & Chr() & "/data/lwfd/client/{CLIENTID}/{TYPE}/{YYYYMMDD}" & Chr() & _
" filename=" & Chr() & "{TYPE}_{CCC}_{YYYYMMDDHH}_{FFF}_{TT}.grib2" & Chr() & " >" & "</grid>" & Chr() fst.WriteText " </client>" & Chr() End If Next Row MsgBox ("Job Done") End Sub
references:
http://stackoverflow.com/questions/2524703/save-text-file-utf-8-encoded-with-vba
http://stackoverflow.com/questions/31435662/vba-save-a-file-with-utf-8-without-bom
http://stackoverflow.com/questions/4143524/can-i-export-excel-data-with-utf-8-without-bom
http://www.tutorialspoint.com/vba/vba_text_files.htm
用VBA读取Excel表格输出到格式化的xml文件中的更多相关文章
- C#读取Excel表格数据到DataGridView中和导出DataGridView中的数据到Excel
其实想在datagridview中显示excel表格中的数据跟读取数据库中的数据没什么差别,只不过是创建数据库连接的时候连接字段稍有差别. private void btnShow_Click(obj ...
- VBA取得EXCEL表格中的行数和列数
VBA取得EXCEL表格中的行数和列数 初学EXCEL宏的童鞋,总是很想知道表格中含有数据的行数和列数,尤其是行数和列数不确定的情况下.这样可以避免很多的错误,并且可以提高效率.但每次用到的时候到网上 ...
- Java读取excel表格
Java读取excel表格 一般都是用poi技术去读取excel表格的,但是这个技术又是什么呢 什么是Apache POI? Apache POI是一种流行的API,它允许程序员使用Java程序创建, ...
- C#读取Excel表格中数据并返回datatable
在软件开发的过程中,经常用到从excel表格中读取数据作为数据源,以下整理了一个有效的读取excel表格的方法. DataTable GetDataTable(string tableName,str ...
- 利用java反射机制实现读取excel表格中的数据
如果直接把excel表格中的数据导入数据库,首先应该将excel中的数据读取出来. 为了实现代码重用,所以使用了Object,而最终的结果是要获取一个list如List<User>.Lis ...
- 读取Excel表格中数据原型
写下这篇博客来记录自己的工作,这部分功能是读取Excel表格中的数据,并通过c#中的datagridview控件将读取的数据显示出来.为了方便用户,我设计了一个read按钮,用户点击这个按钮,会弹出打 ...
- C#读取Excel表格的数据
1.创建工程后,需要下载 EPPlus.dll 添加到工程中,这里有一个下载地址:https://download.csdn.net/download/myunity/10784634 2.下面仅实现 ...
- C# 读取Excel表格内容,以及NPOI的使用
在实际的开发中,我们可能需要读写word或者Excel的内容,在我开发的项目中,需要读取Excel的内容,并将相对应的内容存储到数据库中,这里简单跟大家分享一下,希望能够帮助一些人. 我相信在读写wo ...
- 读取Excel表格日期类型数据的时候
用POI读取Excel数据:(版本号:POI3.7) 1.读取Excel 2.Excel数据处理: Excel存储日期.时间均以数值类型进行存储,读取时POI先判断是是否是数值类型,再进行判断转化 1 ...
随机推荐
- ASP.NET 之深入浅出Session和Cookie
在做人事档案管理系统中,对于Session和Cookie的使用后理解更加深刻了,下面对本知识点总结学习. Session是什么? 简单来说就是服务器给客户端的一个编号.当一台WWW服务器运行时,可能有 ...
- Drupal 实战
<Drupal 实战> 基本信息 作者: 葛红儒 丛书名: 实战系列 出版社:机械工业出版社 ISBN:9787111429999 上架时间:2013-6-28 出版日期:2013 ...
- Hibernate 入门的第一个程序
一. Hibernate介绍 Hibernate是基于对象/关系映射(ORM,Object/Relational Mapping)的一个解决方案.ORM方案的思想是将对象模型表示的对象映射到关 ...
- Embedded tomcat 7 servlet 3.0 annotations not working--转
Question: I have a stripped down test project which contains a Servlet version 3.0, declared with an ...
- JQuery window、document、 body (转)
转自:http://www.cnblogs.com/luhe/archive/2012/11/08/2760619.html 我电脑屏幕分辨率:1440 * 900 最大化浏览器,刷新浏览器 al ...
- Because the people who are crazy enough to think they can change the world, are the ones who do.
Here's to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the square h ...
- 一个好用的Python备份mysql的脚本
前几天打算用Python写一个mysql脚本,上Google看了下老外写的,写的挺好的,原地址在http://tecadmin.net/python-script-for-mysql-database ...
- JavaScript基础(二)
一.外部引用语法<script src="script.js"></script> 二.在页面中的位置 1.我们可以将JavaScript代码放在html文 ...
- GUID的广泛使用
GUID(Global unique identifier)全局唯一标识符,它是由网卡上的标识数字(每个网卡都有唯一的标识号)以及 CPU 时钟的唯一数字生成的的一个 16 字节的二进制值. GUID ...
- Design Pattern ——Factory Method&Abstract Factory
今天开始复习设计模式.设计模式相关的资料有很多,概念性的东西就画个图就可以了.把关注点放在例子上,设计模式还是要使用中才有感受. 从Factory Method&Abstract Factor ...