用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 ...
随机推荐
- linux之utime函数解析
[lingyun@localhost utime]$ ls hello utime.c world [lingyun@localhost utime]$ cat utime.c /******* ...
- <Win32_8>由浅入深——滚动条
滚动条在Win32程序中是非常常见的一个控件,它的功能和地位也就不言而喻了,在文本输出中算是一个难点…… 我将借用P先生的思路讲述两种不同风格滚动条,下面切入主题:(实例程序都是显示一张位图 当然, ...
- OpenRisc-41-or1200的cache模块分析
引言 为CPU提供足够的,稳定的指令流和数据流是计算机体系结构设计中两个永恒的话题.为了给CPU提供指令流,需要设计分支预测机构,为了给CPU提供数据流,就需要设计cache了.其实,无论是insn还 ...
- JAVA/PHP/C#版RSA验签--转
本文是上一篇文章的兄弟篇,上篇文章介绍了客户端的sdk中如何基于JAVA/PHP/C#使用RSA私钥签名,然后服务端基于JAVA使用RSA公钥验签,客户端签名/服务端验签的模式只能帮助服务端检查客户端 ...
- Linux下用rpm方式安装MySQL
1.MySQL下载地址. www.mysql.com/downloads/mysql-4.0.html 下载MySQL 5.1版本的2个包(根据你的实际需求下载所需要的包): MySQL-server ...
- SpringMVC08转发和重定向
public class User { private String name; private Integer age; public String getName() { return name; ...
- ExecuteNonQuary接收存储过程的输出类型的变量的值
1.设置所调用的存储过程需要的参数 public decimal CreateOrder(string orderId, int userId, string address) { SqlParame ...
- 百度背景画面切换效果,js做
<!DOCTYPE html><html><head> <title>baidu</title> <meta charset=&quo ...
- Cannot modify header information - headers already sent by
有时候你在使用 header("Location: http://localhost/myblog/index.php/manager/listview");的时候会出现这个问题, ...
- Cacti监控Linux主机
1 要监视一台Linux主机,需要在被监控的主机上安装net-snmp相关软件包,CentOS安装可使用“yum -y install net-snmp”命令:# yum -y install net ...