发布于 2011-07-07 19:17:55
19楼
我工程中的一个例子,按输入的日期查询Excel表中的相应的行,然后从该行开始(共20行)读入到WinCC中的显示表中。
'inquiry the Row No. according to the required date
Dim objExcelApp,oWorkBook
Dim oFindRowNo,vDate,oVar,i,j
On Error Resume Next
Set objExcelApp=CreateObject("Excel.Application")
objExcelApp.Visible=False
Set oWorkBook=objExcelApp.Workbooks.Open("d:\GuangXi_001\OwnWork\DATA_Table_V1201.xls")
vDate=ScreenItems("Acquisition_Date").OutputValue
oFindRowNo=oWorkBook.ActiveSheet.Columns(13).Find("*"&vDate).Row
'according to excel date format, prefix"*" needed
If CStr(oFindRowNo)="" Then
MsgBox("Not find? please try another date again!!!")
Else
'MsgBox("Continue ?Row No.="&oFindRowNo)
ScreenItems("Excel_TableBeginRowNo").OutputValue=oFindRowNo
'read data from Excel and write to WinCC Screen I/O fields
For i=1 To 20
For j=1 To 14
oVar="Production_Data_Table_Row"&CStr(i)&".Column_"&CStr(j)
HMIRuntime.SmartTags(oVar).value=objExcelApp.Cells(oFindRowNo+i-1,j).Value
Next
Next
ScreenItems("Excel_TableCurrentRowNo").OutputValue=oFindRowNo+19
End If
objExcelApp.Workbooks.Close
objExcelApp.Quit
Set objExcelApp=Nothing
Set oWorkBook=Nothing