基于VB语言的EXCEL、CST以及HFSS等的二次开发
代码1:vb创建xls表,并写入内容
Set ExcelApp = CreateObject("Excel.Application") '创建EXCEL对象Set ExcelBook = ExcelApp.Workbooks.Add
Set ExcelSheet = ExcelBook.Worksheets(1) '添加工作页ExcelSheet.Activate '激活工作页
ExcelApp.DisplayAlerts = False
="sheet1"
ExcelSheet.Range("A1").Value = 100 '设置A1的值为100
ExcelBook.SaveAs "d:\test.xls" '保存工作表
msgbox "d:\test.xls创建成功!"
ExcelBook.close
set excelApp=nothing
set ExcelBook=nothing
set ExcelSheet=nothing
将以上代码copy到记事本存为"writexls.vbs"文件,可运行测试
代码2:读execel文件
Set ExcelApp = CreateObject("Excel.Application") '创建EXCEL对象Set ExcelBook = ExcelApp.Workbooks.open("d:\test.xls")
Set ExcelSheet = ExcelBook.Worksheets(1)
msgbox ExcelSheet.Range("A1").Value
将以上代码copy到记事本存为"readxls.vbs"文件,可运行测试
代码3:上述代码联合调试
Dim ExcelApp,ExcelBook,ExcelSheet
Set ExcelApp = CreateObject("Excel.Application") '创建EXCEL对象Set ExcelBook = ExcelApp.Workbooks.Add
Set ExcelSheet = ExcelBook.Worksheets(1) '添加工作页ExcelSheet.Activate '激活工作页
ExcelApp.DisplayAlerts = False
="sheet1"
ExcelSheet.Range("A1").Value = 100 '设置A1的值为100
ExcelBook.SaveAs "D:\Study\VBS\Book1.xls" '保存工作表
msgbox "d:\Book1.xls创建成功!"
ExcelBook.close
set excelApp=nothing
set ExcelBook=nothing
set ExcelSheet=nothing
'ExcelApp.WorkBooks.Close
'ExcelApp.Quit
Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Visible = True
'创建EXCEL对象
Set ExcelBook = ExcelApp.Workbooks.open("D:\Study\VBS\Book1.xls")
Set ExcelSheet = ExcelBook.Worksheets(1)
msgbox ExcelSheet.Range("A1").Value
ExcelApp.WorkBooks.Close
ExcelApp.Quit
若对支持VB脚本的软件进行二次开发,上述描述有借鉴意义。
例如CST,HFSS 等电磁软件(以及AutoCAD,ANSYS等)就可如此外部调用。
代码4:VBS调用CST进行运算
Dim app
Set app = CreateObject("CSTStudio.Application")
Dim mws
Set mws = app.OpenFile("D:\VBS\1.cst")
'mws.Visible = False
With mws
.DeleteResults
.Rebuild
.Solver.Start
.Save
End With
mws.Quit
代码5:VBS调用CST进行运算
Dim fs
Set fs =createobject("scripting.filesystemobject")
Dim ts
Set ts=fs.opentextfile("opt.txt",1,true)
Dim path
path=ts.readall
ts.close
'msgbox path
Dim app
Set app = CreateObject("CSTStudio.Application")
Dim mws
Set mws = app.OpenFile(path)
Dim chang
Dim kuan
Dim gao
'Dim result(30)
Set ts=fs.opentextfile("chang1.txt",1,true)
chang=ts.readline
'msgbox chang
ts.close
Set ts=fs.opentextfile("kuan1.txt",1,true)
kuan=ts.readline
'msgbox kuan
ts.close
Set ts=fs.opentextfile("gao1.txt",1,true)
gao=ts.readline
'msgbox gao
ts.close
With mws
.DeleteResults
.StoreParameter "Lg", chang
.StoreParameter "Ls", kuan .StoreParameter "Lcross",
gao
.Rebuild
.Solver.Start
.Save
End With
mws.Quit
Window系统下,C++调用VBS的参考方法:WinExec(" wscript.exe
D:\Study\VBS\Book.vbs", SW_SHOWNORMAL);。