西门子有做个WinCC、Excel、VBA、脚本、连通性软件包的例子,也就是在EXCEL宏指令做日报表,现在测试通了,选择日期会有数据出来,关键是有两个问题:一是WINCC中变量数据是25.6,那么打开EXCEL的数据变成了25.0,EXCEL的单元格式也没错设置成1234.0,EXCEL中总是小数点后面的数据显示不出来是怎么回事?二是假设现在是上午整点10点开始运行WINCC并触发变量记录数据了,但是到EXCEL中变成了00点那一行数据,如图;以下是EXCEL的宏指令代码:
Dim sPro, sDsn, sSer, sCon, sSql
Dim conn, oRs, oCom
Dim DSNName
Dim i As Integer
Dim sStart, sStop As String
Sub get_wincc_data()
'--Get Database DSN name-----------------------------------
Set DSNName = CreateObject("CCHMIRuntime.HMIRuntime")
sDsn = DSNName.Tags("@DatasourceNameRT").Read
'--build connection string-----------------------------------
sPro = "Provider=WinCCOLEDBProvider.1;"
sDsn = "Catalog=" & sDsn & ";"
sSer = "Data Source=HH\WinCC"
sCon = sPro & sDsn & sSer
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = sCon
conn.CursorLocation = 3
conn.Open
Set oRs = CreateObject("ADODB.Recordset")
Set oCom = CreateObject("ADODB.Command")
oCom.CommandType = 1
Set oCom.ActiveConnection = conn
'查询启止时间
sStart = Year(DTPicker1.Value) & "-" & Month(DTPicker1.Value) & "-" & Day(DTPicker1.Value) & " 00:00:00"
sStop = Year(DTPicker1.Value) & "-" & Month(DTPicker1.Value) & "-" & Day(DTPicker1.Value) & " 23:00:00"
'转为UTC时间
sStart = DateAdd("h", -8, CDate(sStart))
sStop = DateAdd("h", -8, CDate(sStop))
'读取Fan1_T1
sSql = "Tag:R,('ProcessValueArchive\A'),'" & sStart & "','" & sStop & "' order by datetime"
oCom.CommandText = sSql
Set oRs = oCom.Execute
If (oRs.EOF) Then
oRs.Close
Else
oRs.MoveFirst
i = 0
Do While Not oRs.EOF
Sheet1.Cells(i + 4, 2) = oRs.Fields("RealValue").Value
oRs.MoveNext
i = i + 1
Loop
oRs.Close
End If
Set oRs = Nothing
Set conn = Nothing
End Sub
Private Sub DTPicker1_Change()
clear_cell '清除已经填充的数据
get_wincc_data '读取WinCC历史数据
End Sub
Sub clear_cell()
For i = 4 To 27
For j = 2 To 5
Cells(i, j) = ""
Next j
Next i
End Sub
图片说明:
最佳答案
EXCEL中小数点后面的数据显示不出来的问题先检查一下WinCC中写到数据库中的数据、可以用历史趋势控件的表格显示,再就是Excel表格的格式。时间不对应的问题,看脚本中往Excel中写并未根据时间分配行号,而是按读取顺序写入,只读到一条就写到了第一行,不知是不是这个原因。
提问者对于答案的评价:
谢谢
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc157755.html