我是这样做的,通过VBS把WINCC实时数据些到我建立好的SQL数据库中,然后在WINCC界面上做一个查询按钮,按你所要的日期或时间从数据库中读出相应数据。下面,我把查询的部分代码贴出。
Sub OnLButtonDown(ByVal Item, ByVal Flags, ByVal x, ByVal y)
Dim strDate,strTime
Dim strcn,cn
Dim rs
Dim strSQL
Dim comm
Dim i
‘建立数据库连接
strcn="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DBNAEM;Data Source=dbserver\wincc"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Set comm=CreateObject("ADODB.Command")
cn.ConnectionString=strcn
cn.cursorlocation=3
cn.Open
comm.commandtype=1
comm.activeconnection=cn
'**********************************************************
'**********************************************************
Dim StrYear
Set StrYear = HMIRuntime.Tags("StrYear")
stryear.Read
Dim StrMonth
Set StrMonth = HMIRuntime.Tags("StrMonth")
strmonth.Read
Dim StrDay
Set StrDay = HMIRuntime.Tags("StrDay")
strday.Read
strDate=stryear.Value +"-"+strmonth.Value+"-"+strday.Value
Dim StrHour
Set StrHour = HMIRuntime.Tags("StrHour")
StrHour.Read
Dim StrMinute
Set StrMinute = HMIRuntime.Tags("StrMinute")
StrMinute.Read
strTime=StrHour.Value+":"+StrMinute.Value
'*************************************************************
建立EXCEL对象
Dim objexcelApp
Set objexcelApp=CreateObject("excel.application")
With objexcelApp
.visible=True
.workbooks.open"d:\SPSQLServer_2008\ExcelReport\SOURCE.xls"打开EXCEL模板
.activeworkbook.activesheet.select
.displayalerts=False
End With
'****************************************************************
查询数据库中相应数据,然后写到相应的EXCEL单元格中。
i=4 表示第四行
strSQL="select * from Station_1 where St1_Date='"&strDate&"'and St1_Time='"&StrTime&"'"
comm.commandtext=strSQL
Set rs=comm.execute
If Not rs.eof Then
With objexcelApp.worksheets(1)
objexcelApp.Worksheets(1).cells(i,3).value=rs.fields(0).value
objexcelApp.Worksheets(1).cells(i,4).value=rs.fields(1).value
objexcelApp.Worksheets(1).cells(i,5).value=rs.fields(2).value
objexcelApp.Worksheets(1).cells(i,6).value=rs.fields(3).value
objexcelApp.Worksheets(1).cells(i,7).value=rs.fields(4).value
objexcelApp.Worksheets(1).cells(i,8).value=rs.fields(5).value
objexcelApp.Worksheets(1).cells(i,9).value=rs.fields(6).value
objexcelApp.Worksheets(1).cells(i,10).value=rs.fields(7).value
objexcelApp.Worksheets(1).cells(i,11).value=rs.fields(8).value
objexcelApp.Worksheets(1).cells(i,12).value=rs.fields(9).value
objexcelApp.Worksheets(1).cells(i,13).value=rs.fields(10).value
objexcelApp.Worksheets(1).cells(i,14).value=rs.fields(11).value
objexcelApp.Worksheets(1).cells(i,15).value=rs.fields(12).value
objexcelApp.Worksheets(1).cells(i,16).value=rs.fields(13).value
objexcelApp.Worksheets(1).cells(i,17).value=rs.fields(14).value
objexcelApp.Worksheets(1).cells(i,18).value=rs.fields(15).value
objexcelApp.Worksheets(1).cells(i,19).value=rs.fields(16).value
objexcelApp.Worksheets(1).cells(i,20).value=rs.fields(17).value
objexcelApp.Worksheets(1).cells(i,21).value=rs.fields(26).value
objexcelApp.Worksheets(1).cells(i,22).value=rs.fields(20).value
objexcelApp.Worksheets(1).cells(i,23).value=rs.fields(21).value
objexcelApp.Worksheets(1).cells(i,24).value=rs.fields(22).value
objexcelApp.Worksheets(1).cells(i,25).value=rs.fields(23).value
objexcelApp.Worksheets(1).cells(i,26).value=rs.fields(25).value
End With
End if
i=5
。。。。。。。。。。
。。。。。。。。。。
i=N
rs.close
cn.close
Set rs=Nothing
Set comm=Nothing
Set cn=Nothing
Set objexcelApp=Nothing
End Sub
管理员注:本帖已被纳入此次探讨发帖整理之方案汇总,请
点此详阅