A0579_在WinCC中如何使用VBS读取变量归档数据到EXCEL

 我来答
天道送葬者
2012-05-07 · TA获得超过349个赞
知道小有建树答主
回答量:528
采纳率:0%
帮助的人:222万
展开全部
读取变量归档的查询语句是:
TAG:R, <ValueID or ValueName>,<TimeBegin>,<TimeEnd>[,<SQL_clause>][,<TimeStep>]
其中TimeStep指的是时间间隔

Sub OnLButtonDown(ByVal Item, ByVal Flags, ByVal x, ByVal y)
Dim sPro
Dim sDsn
Dim sSer
Dim sCon
Dim sSql

Dim conn
Dim oRs
Dim oCom
Dim oItem

'Dim m,n,s

'connection string
sPro = "Provider=WinCCOLEDBProvider.1;"
sDsn = "Catalog=CC_XXXXX_12_03_10_08_18_49R;" '数据库名
sSer = "Data Source=.\WinCC"
sCon = sPro + sDsn + sSer
'sCon="Provider=WinCCOLEDBProvider.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SSEJRecord;Data Source=.\WINCC"
'MsgBox "set sCon"

'sSQL为查询命令
sSql = "TAG:R,'ProcessValueArchive\AnalogTag','0000-00-00 00:00:20.000','0000-00-00 00:00:00.000'"
'sSql = "ALARMVIEW:SELECT * from AlgViewChs"
'MsgBox "Set SQL"

'建立连接
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = sCon
conn.CursorLocation = 3
conn.Open
'MsgBox "conn Open"

'进行查询
Set oRs = CreateObject("ADODB.Recordset")
Set oCom = CreateObject("ADODB.Command")
oCom.CommandType = 1
Set oCom.ActiveConnection = conn
oCom.CommandText = sSql
'MsgBox "set command"

'填充记录集
Set oRs = oCom.Execute
'm = oRs.Fields.Count
MsgBox oRs.EOF

If(oRs.EOF) Then
oRs.Close
Else
oRs.MoveFirst
Do While Not oRs.EOF
HMIRuntime.Tags("AlarmText1").Write CStr(oRs.Fields(17).Value)
oRs.MoveNext
Loop
oRs.Close
End If

Set oRs = Nothing
conn.Close
Set conn = Nothing

'MsgBox "end"
End Sub
下面是WINCC读写EXCEL的脚本
写:
Sub OnOpen()

Dim objComboBox

Set objComboBox = ScreenItems("控件1")
objComboBox.Clear()

Dim objExcelApp,oWorkBook
Set objExcelApp = CreateObject("Excel.Application")
objExcelApp.Visible = False
Set oWorkBook = objExcelApp.Workbooks.Open("c:\1.xls")

objComboBox.AddItem objExcelApp.Cells(1, 1).Value
objComboBox.AddItem objExcelApp.Cells(1, 2).Value
objComboBox.AddItem objExcelApp.Cells(1, 3).Value

objComboBox.SelText = "选项1"

objExcelApp.Workbooks.Close
objExcelApp.Quit
Set objExcelApp = nothing
End Sub

读:
Sub OnLButtonDown(Byval Item, Byval Flags, Byval x, Byval y)

Dim objExcelApp,oWorkBook
Set objExcelApp = CreateObject("Excel.Application")
objExcelApp.Visible = False
Set oWorkBook = objExcelApp.Workbooks.Open("c:\1.xls")
Dim iBlankLine
iBlankLine = oWorkBook.ActiveSheet.Columns(1).Find("").Row '查找第一列的空行
'MsgBox iBlankLine
objExcelApp.Cells(iBlankLine, 1).Value = "tag1"
objExcelApp.Cells(iBlankLine, 2).Value = "tag2"
objExcelApp.Cells(iBlankLine, 3).Value = "tag3"
oWorkBook.Save
objExcelApp.Workbooks.Close
objExcelApp.Quit
Set objExcelApp = nothing
'Set oWorkBook =objExcelApp.Workbooks.Add
'oWorkBook.SaveAs "D:\ExcelExample.xls"

End Sub

以上都是我写的 现在还在用 这两部分合起来就能完成你的功能了
小亨top
2014-09-04 · 超过34用户采纳过TA的回答
知道答主
回答量:41
采纳率:0%
帮助的人:87.4万
展开全部
当枯叶化作灰烬的尘埃
旅游团的休息室来看,结婚的日子
啊,我的失却了的朝气、
会像孩子一样大喊大叫,父啊我愿在此居住,
它们也曾经耸立在干爽的陆地,
毕的勤俭护儿女,哈哈
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式