读写配置文件的函数,适用于表格做的配置文件
程序员文章站
2024-03-19 11:29:10
...
日常编程过程中,经常需要将一些参数或是客户设定的内容,放到配置文件中,在程序执行过程中,查以查找这些配置,以便完成参数化的操作。下面的函数即是为了方便的读、写配置表中的各项内容而写的,可以从表格中快速的找到相关内容进行读取或写入的操作。具体见以下程序代码:
函数代码
Public Function GetINIData(DataFie As String, Optional FiePos As String = "ByCol", Optional Sh As String = "INI")
'读取配置内容,默认内容在项的下一行或是下一列
Dim Rng As Range
If Sh = "" Then Sh = ActiveSheet.Name
With Sheets(Sh)
If FiePos = "ByCol" Then '查找字段所在行或列,默认为配置项写在A列中
Set Rng = .Range("A:A").Find(DataFie, lookat:=xlWhole)
If Not Rng Is Nothing Then GetINIData = Rng.Offset(0, 1)
Else
Set Rng = .Range("1:1").Find(DataFie, lookat:=xlWhole)
If Not Rng Is Nothing Then GetINIData = Rng.Offset(1)
End If
End With
End Function
Public Function SetINIData(DataFie As String, Optional FiePos As String = "ByCol", Optional Sh As String = "INI")
'写配置内容,默认内容在项的下一行或是下一列
'项和内容按“项=内容”填写
Dim Rng As Range
Dim sp() As String
sp = Split(DataFie, "=")
If UBound(sp) = 0 Then Exit Function
If Sh = "" Then Sh = ActiveSheet.Name
With Sheets(Sh)
If FiePos = "ByCol" Then '查找字段所在行或列
Set Rng = .Range("A:A").Find(sp(0), lookat:=xlWhole)
If Not Rng Is Nothing Then Rng.Offset(0, 1) = sp(1): SetINIData = True
Else
Set Rng = .Range("1:1").Find(sp(0), lookat:=xlWhole)
If Not Rng Is Nothing Then Rng.Offset(1) = sp(1): SetINIData = True
End If
End With
End Function
调用的例子
GetINIData "数据库名称","ByCol","INI"
GetINIData "阶梯一"
SetINIData "基础一提成=6","ByCol","INI"
SetINIData "实习比例=4"
配置表的例子
主配置项在A列,内容在B列
——专注办软件的二次开发及培训,你有问题,我有思路!
——微博、微信、CSDN同号:w_dexu。
——转载请注明出处!
扫码加微信