用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 ...
随机推荐
- Struts2学习笔记(一):Struts2开发环境的配置
一.Struts2应用所需的jar文件. 开发struts2应用需要依赖的jar文件在解压目录下的lib文件夹里面.开发struts2程序最少需要的jar文件为:struts2-core-2.xx.j ...
- NS CKD
NS 定义:ALB<30:高脂血症:大量蛋白尿>3.5g:浮肿 见于:肾小球肾炎.小血管炎.微血管血栓性疾病.NSAIDs引起的急性间质性肾炎.不见于肾大血管病.间质性.小管性疾病. 并发 ...
- nginx+keepalived实现nginx双主高可用的负载均衡
http://kling.blog.51cto.com/3320545/1253474 一.前言: 在互联网上面,网站为用户提供原始的内容访问,同时为用户提供交互操作.提供稳定可靠的服务,可以给用户带 ...
- [Angular 2] Using ngrx/store and Reducers for Angular 2 Application State
ngrx/store is a library that simplifies common RxJS patterns for managing state and gives you an eas ...
- [Docker] Docker Machine intro
List all the docker machine: docker-machine ls Can check 'ip' and 'status': docker-machine ip defaul ...
- android 支持的语言列表(汇总)
Arabic, Egypt (ar_EG) -----------------------------阿拉伯语,埃及Arabic, Israel (ar_IL) ------------------- ...
- linux怎么给一个普通用户reboot权限?
分四种情况讨论:1.让任何人(包括根本不拥有系统帐号的人)都可以通过控制台reboot在/etc/inittab文件中保留ca::ctrlaltdel:/sbin/shutdown -t3 -r no ...
- UITableView的编辑(插入、删除、移动)
先说两个方法beginUpdates和endUpdates,几点注意事项: 一般我们把行.块的插入.删除.移动写在由这两个方法组成的函数块中.如果你不是在这两个函数组成的块中调用插入.删除.移动方法, ...
- 操作sql - 类型初始值设定项引发异常
这个异常我还是第一次看见,网上有人说,若出现异常,则访问所有的静态成员,均会抛出异常. 在我碰到的问题中,如下代码: ; static private System.Data.DataTable Re ...
- 数据库WMI 0x80041010 如何解决?
在你打开 SQL Server Configuration Manager遇到以下错误的时候,请参考下面提出的解决办法 solution: 打开cmd 命令窗口执行mofcomp.exe " ...