ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 11954|回复: 10

[求助] 请问使用VBA如何读取INI文件内容,并写入到EXCEL中,谢谢

[复制链接]

TA的精华主题

TA的得分主题

发表于 2013-6-26 15:48 | 显示全部楼层 |阅读模式
本帖已被收录到知识树中,索引项:文件操作和FSO
本帖最后由 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]



TA的精华主题

TA的得分主题

发表于 2013-6-26 16:21 | 显示全部楼层
  1. Sub GetTxtFromIni()
  2. Dim fs, F, a$, Fp$
  3. With Application.FileDialog(msoFileDialogFilePicker)
  4.     If .Show <> -1 Then Exit Sub
  5.     Fp = .SelectedItems(1)
  6. End With

  7.         Set fs = CreateObject("Scripting.FileSystemObject")
  8.         Set F = fs.OpenTextFile(Fp, 1)
  9.         a = F.ReadAll
  10.         
  11. Dim regEx, Matches, Match, xrr(1 To 4 ^ 8, 1 To 3), x%, i%
  12. [a1:c1] = Array("User", "Bindlist", "Depict")
  13. brr = Array("\[user\d{1,}\]", "bindlist=.*", "depict=.*")
  14. For i = 0 To 2
  15. Set regEx = CreateObject("VBSCRIPT.REGEXP")
  16.     With regEx
  17.         .Global = True      
  18.         .Pattern = brr(i)
  19.         Set Matches = .Execute(a)
  20.     End With
  21. x = 0
  22.     For Each Match In Matches
  23.         x = x + 1
  24.         xrr(x, i + 1) = Match
  25.     Next
  26. Next
  27. Set regEx = Nothing
  28. [a2].Resize(x, 3) = xrr
  29. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2013-6-26 16:27 | 显示全部楼层
Option Explicit

Sub ReadINI()
    Dim sT As String
    Dim f As Integer, i As Integer
    Dim v As Variant

    f = FreeFile
    i = 1
    Open ThisWorkbook.Path & "\tt.ini" For Input As #f
    Do While Not EOF(f)
        Line Input #f, sT
        If sT <> "" Then
            If Left(sT, 1) = "[" Then
                Cells(i, 1) = sT
            Else
                v = Split(sT, "=")
                Cells(i, 2) = v(0)
                Cells(i, 3) = v(1)
            End If
            i = i + 1
        End If
    Loop
    Close #f
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2013-6-27 08:34 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 heghui 于 2013-6-27 08:36 编辑
gufengaoyue 发表于 2013-6-26 16:21

谢谢,经测试运行代码,可以正确读取到INI内容,但是我需要的格式是在user1在excel中显示如下格式:
是a1 显示user1栏位下的depict,b1显示bindlist
以此类推
另外 ,代码好复杂哦,看不懂,能不能注释下,因为user1下的栏位以后可能还会增加,看明白懂了,后面才可以修改
谢谢啦

TA的精华主题

TA的得分主题

发表于 2013-6-27 10:25 | 显示全部楼层
你自己的方法就是好方法,是专门读写配置文件的API。

TA的精华主题

TA的得分主题

 楼主| 发表于 2013-6-27 10:27 | 显示全部楼层
hnwxm 发表于 2013-6-27 10:25
你自己的方法就是好方法,是专门读写配置文件的API。

是可以读取,但是不知道将读取的格式按照需要的格式定入到EXCEL,请指教,谢谢

TA的精华主题

TA的得分主题

发表于 2013-6-27 10:31 | 显示全部楼层
格式你自己是知道的,Excel的单元格你可以随意写,有什么难的。

TA的精华主题

TA的得分主题

 楼主| 发表于 2013-6-27 10:53 | 显示全部楼层
hnwxm 发表于 2013-6-27 10:31
格式你自己是知道的,Excel的单元格你可以随意写,有什么难的。

是需要用VBA实现,这不懂呀

TA的精华主题

TA的得分主题

发表于 2013-6-27 11:02 | 显示全部楼层
看看这个例子对你是否有所帮助。

vba 操作 ini文件.rar

19.04 KB, 下载次数: 369

TA的精华主题

TA的得分主题

 楼主| 发表于 2013-6-27 14:07 | 显示全部楼层
Moneky 发表于 2013-6-27 11:02
看看这个例子对你是否有所帮助。

谢谢,我先学习下
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-17 02:20 , Processed in 0.048063 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表