Private Sub CommandButton1_Click()
Dim str As String '字符串变量
Dim strc As String '设置Range()中娶属性的参数
Dim strd As String '设置Range()中取数据类型的参数
Dim stre As String '设置Range()中取备注的参数
Dim i As Integer 'For中的循环变量
'str = "工作表sheet1内单元格D6中的值为:" & Worksheets("Sheet1").Range("D6").Value
'循环从excel中取值
For i = 5 To 8 Step 1
strc = "C" + CStr(i)
strd = "D" + CStr(i)
stre = "E" + CStr(i)
str = Worksheets("Sheet1").Range(strd).Value
'MsgBox str
Next i
'调用方法
Call getData
End Sub
'把个行读出来放到相应的数组当中并生成javabean格式到本地的txt文本中(E:\\javabean\\javabean.txt)
Function getData()
Dim strRemark As String
Dim iid As String
Dim strdefine As String
Dim Methodget As String
Dim Meteodset As String
Dim MethodgetRemark As String
Dim MethodsetRemark As String
Dim enterkey As String
Dim PropertyArray() As String
Dim TypesArray() As String
Dim RemarkArray() As String
Dim iNmu As Integer
Dim i As Integer
Const ForReading = 1, ForWriting = 2
Dim fs, f, ts
Dim x As String
iNum = Sheets("sheet1").[C65536].End(xlUp).Row
ReDim Preserve PropertyArray(iNum) As String
ReDim Preserve TypesArray(iNum) As String
ReDim Preserve RemarkArray(iNum) As String
For i = 1 To iNum
PropertyArray(i - 1) = Sheets("sheet1").Range("C" & i).Value
TypesArray(i - 1) = Sheets("sheet1").Range("D" & i).Value
RemarkArray(i - 1) = Sheets("sheet1").Range("E" & i).Value
Next i
' MsgBox RemarkArray(4)
'定义javabean属性和注释。
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CreateTextFile "E:\\javabean\\javabean.txt"
Set f = fs.GetFile("E:\\javabean\\javabean.txt")
Set ts = f.OpenAsTextStream(ForWriting)
For i = 4 To iNum - 1
strRemark = "/**" + vbCrLf + RemarkArray(i) + "." + vbCrLf + "*/"
iid = "private" + " " + TypesArray(i) + " " + PropertyArray(i)
strdefine = "private" + " " + TypesArray(i) + " " + PropertyArray(i) + ";"
ts.Writeline strRemark
ts.Writeline strdefine
' MsgBox strRemark
' MsgBox strdefine
Next i
ts.Writeline
For i = 4 To iNum - 1
strRemark = "/**" + RemarkArray(i) + "." + "*/"
iid = "private" + " " + TypesArray(i) + " " + PropertyArray(i)
strdefine = "private" + " " + TypesArray(i) + " " + PropertyArray(i)
'生成setter和getter的注释
MethodgetRemark = "/**" + vbCrLf + "得到" + PropertyArray(i) + "." + vbCrLf + "@return" + " " + PropertyArray(i) + "." + vbCrLf + "*/"
MethodsetRemark = "/**" + vbCrLf + "设置" + PropertyArray(i) + "." + vbCrLf + "@param" + " " + StrConv(PropertyArray(i), 2) + "." + vbCrLf + "*/"
'javabean的setter和getter方法的定义
'StrConv(PropertyArray(i), 2),将字符串转换成小写
Methodget = "public" + " " + TypesArray(i) + " " + "get" + PropertyArray(i) + " " + "()" + " " + "{" + "return" + " " + PropertyArray(i) + ";" + "}"
Methodset = "public" + " " + "void" + " " + "set" + PropertyArray(i) + " " + "(" + TypesArray(i) + " " + StrConv(PropertyArray(i), 2) + ")" + " " _
+ "{" + "this." + PropertyArray(i) + " " + "=" + " " + StrConv(PropertyArray(i), 2) + ";" + "}"
ts.Writeline MethodgetRemark
ts.Writeline Methodget
ts.Writeline MethodsetRemark
ts.Writeline Methodset
' MsgBox strRemark
' MsgBox strdefine
Next i
ts.Close
Set ts = Nothing
Set f = Nothing
Set fs = Nothing
MsgBox "生成javabean文本成功,目录:(E:\\javabean\\javabean.txt)"
End Function