|
本帖最后由 heghui 于 2013-6-26 15:53 编辑
大家好:
有如下格式的INI文件,需要使用VBA读取里面的资料,并写入到EXCEL中:
[user1]
bindlist=职员
depict=管理部
[user2]
bindlist=职员
depict=管理部
[user3] //user后面的数字一直到三百多
bindlist=职员
depict=管理部
使用如下代码可以读取单个的[user]并写入到EXCEL中,请问如何才能将INI文件中的所有 user[1-n]全部读取,并依次写入到excel中,谢谢
[code=vb]Option Explicit
Private Declare Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" _
Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long
Public Function ReadFromIni(ByVal IniFile As String, ByVal Section As String, ByVal Key As String, ByVal DefaultValue As String) As String
Dim strRtn As String
strRtn = Space(256)
Dim lngRtn As Long
lngRtn = GetPrivateProfileString(Section, Key, DefaultValue, strRtn, 255, IniFile)
If lngRtn > 0 Then
strRtn = Trim(strRtn)
ReadFromIni = Mid(strRtn, 1, Len(strRtn) - 1)
Else
ReadFromIni = DefaultValue
End If
End Function
Sub Main()
Dim strIniFile As String
strIniFile = ActiveWorkbook.Path & "\user.ini"
Dim strSection As String
strSection = "user1"
Dim strDepict As String
strDepict = "depict"
Dim strName As String
strName = "bindlist"
Range("a2") = ReadFromIni(strIniFile, strSection, strUser, "")
Range("b2") = ReadFromIni(strIniFile, strSection, strName, "")
End sub[/code]
|
|