如何通過vbs在wincc的畫面中寫入excel中r1c1位的字符串
劉玉蓉
發(fā)布于2014-10-28 17:42
1
0
標(biāo)簽:
在wincc畫面中建立一個i/o域,如何用vbs腳本寫入(讀出)excel中的數(shù)值,只要有一個位置r1c1的數(shù)值就可以了。
新手,一點方向都沒有
請問:
1、腳本是在全局腳本寫還是在對象屬性寫
2、腳本的基本例子給一個,越詳細(xì)越好
3、鏈接的文本看了不少,沒有方向
佳答案
本人做的源代碼:
Sub OnClick(ByVal Item)
Dim fso,myfile
Set fso = CreateObject("scripting.FileSystemObject")
Set MyFile = fso.GetFile("d:\data.xlsx")
Dim ObjExcelApp
Set objExcelApp = CreateObject("Excel.Application")
"objExcelApp.Visible = True
objExcelApp.Workbooks.Open MyFile
"上面的程序段是為了打開d盤excel文件
Dim aa_data
objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue = HMIRuntime.Tags("aa").read
Dim i,j
If objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue>1.0 Then
i=2
j=6
End If
If objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue>0.8 And objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue<1.0 Then
i=7
j=11
End If
If objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue>0.6 And objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue<0.8 Then
i=12
j=16
End If
If objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue<0.6Then
i=17
j=21
End If
"判斷數(shù)據(jù)保存位置
Do While i<j
objExcelApp.worksheets ("sheet1").Cells(i, 1).VAlue =objExcelApp.worksheets ("sheet1").Cells(i+1, 1).VAlue
objExcelApp.worksheets ("sheet1").Cells(i, 2).VAlue = objExcelApp.worksheets ("sheet1").Cells(i+1, 2).VAlue
i=i+1
Loop
objExcelApp.worksheets ("sheet1").Cells(i, 1).VAlue =HMIRuntime.Tags("yy").read
objExcelApp.worksheets ("sheet1").Cells(i, 2).VAlue =HMIRuntime.Tags("xx").read
"寫數(shù)據(jù)到excel表格
objExcelApp.ActiveWorkbook.Save
"保存表格
Dim cons_data,ax1_data,ax2_data,ax3_data,ax4_data,ax5_data,ax6_data
Set cons_data=HMIRuntime.Tags("cons")
Set ax1_data=HMIRuntime.Tags("ax1")
Set ax2_data=HMIRuntime.Tags("ax2")
Set ax3_data=HMIRuntime.Tags("ax3")
Set ax4_data=HMIRuntime.Tags("ax4")
Set ax5_data=HMIRuntime.Tags("ax5")
Set ax6_data=HMIRuntime.Tags("ax6")
cons_data.Value = objExcelApp.worksheets ("sheet1").Cells(23,7).value
ax1_data.Value = objExcelApp.worksheets ("sheet1").Cells(23, 6).value
ax2_data.Value = objExcelApp.worksheets ("sheet1").Cells(23, 5).value
ax3_data.Value = objExcelApp.worksheets ("sheet1").Cells(23, 4).value
ax4_data.Value = objExcelApp.worksheets ("sheet1").Cells(23, 3).value
ax5_data.Value = objExcelApp.worksheets ("sheet1").Cells(23, 2).value
ax6_data.Value = objExcelApp.worksheets ("sheet1").Cells(23, 1).value
"上面的作用是將Excel表格中的數(shù)據(jù)讀到wincc,存到臨時變量里面
objExcelApp.Workbooks.Close
objExcelApp.Quit
Set ObjEXceLapp = Nothing
"到這里為止,關(guān)閉剛才打開的excel程序了
cons_data.Write
ax1_data.Write
ax2_data.Write
ax3_data.Write
ax4_data.Write
ax5_data.Write
ax6_data.Write
"后是把讀到的excel值從臨時變量中寫入它對應(yīng)的wincc變量中
End Sub