基于VB语言的EXCEL和CST以及AutoCAD与ANSYS等的二次开发

发布时间:2011-06-02 12:47:30   来源:文档文库   
字号:

基于VB语言的EXCELCST以及HFSS等的二次开发

代码1vb创建xls表,并写入内容
Set    ExcelApp   =   CreateObject("Excel.Application")       '创建EXCEL对象   
Set    ExcelBook =   ExcelApp.Workbooks.Add  
Set    ExcelSheet    =    ExcelBook.Worksheets(1)      '添加工作页
ExcelSheet.Activate                               '激活工作页
ExcelApp.DisplayAlerts    =    False  
ExcelSheet.Name="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 
ExcelSheet.Name="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脚本的软件进行二次开发,上述描述有借鉴意义。例如CSTHFSS等电磁软件(以及AutoCADANSYS)就可如此外部调用。
代码4VBS调用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
代码5VBS调用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);

本文来源:https://www.2haoxitong.net/k/doc/17ab0d2f7375a417866f8fa5.html

《基于VB语言的EXCEL和CST以及AutoCAD与ANSYS等的二次开发.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式