21.Wincc控件制作报表以及导出EXCEL方法介绍

标签:
winccmshgrid报表导出excelvbs |
分类: 西门子软硬件 |
假设希望做这样一个报表界面,可以根据日期查询:
希望导出的excel报表文件在C盘根目录,是这个样子:
接下来我们一步步实现这个功能。
1.
注意开放sa用户,设置sa用户有管理report数据库权限,使用sql登陆模式。
2.
Option Explicit
Function action
'添加纪录
Dim T1,T2,P1,P2,F1,F2,L1,L2,A1,A2,S1,S2
Dim ors,conn,con,ssql,ocom
Dim PCName
PCName=hmiruntime.Tags("@LocalMachineName").Read
T1=HMIRuntime.Tags("温度1").Read
T2=HMIRuntime.Tags("温度2").Read
P1=HMIRuntime.Tags("压力1").Read
P2=HMIRuntime.Tags("压力2").Read
F1=HMIRuntime.Tags("流量1").Read
F2=HMIRuntime.Tags("流量2").Read
L1=HMIRuntime.Tags("液位1").Read
L2=HMIRuntime.Tags("液位2").Read
A1=HMIRuntime.Tags("分析仪1").Read
A2=HMIRuntime.Tags("分析仪2").Read
S1=HMIRuntime.Tags("转速1").Read
S2=HMIRuntime.Tags("转速2").Read
con="Provider = SQLOLEDB.1;password = sa;user id = sa;Initial Catalog =Report;Data Source = " & PCName & "\WINCC"
Set conn=CreateObject("ADODB.Connection")
conn.ConnectionString=con
conn.Cursorlocation=3
conn.open
ssql="insert into Report(CurDateTime,T1,T2,P1,P2,F1,F2,L1,L2,A1,A2,S1,S2) values(Getdate()," _
Set ors=CreateObject("ADODB.RecordSet")
Set ocom=CreateObject("ADODB.Command")
Set ocom.activeconnection=conn
ocom.CommandType=1
ocom.CommandText=ssql
Set ors=ocom.Execute
Set ors=Nothing
conn.close
Set conn=Nothing
End Function
脚本中的函数不做解释。
3.
4.
Sub OnClick(ByVal
Item)
Dim sdate
Dim conn
Dim ssql
Dim ors
Dim ocom
Dim scon
Dim DBGrid
Dim ADODC
Dim syear
Dim smonth
Dim sday
Dim PCName
PCName=HMIRuntime.Tags("@LocalMachineName").Read
syear=CStr(Year(ScreenItems("DTPicker").value))
If Month(ScreenItems("DTPicker").value)<10 Then
Else
End If
If Day(ScreenItems("DTPicker").value)<10 Then
Else
End If
sdate=syear & "/" & smonth & "/" & sday
scon="Provider = SQLOLEDB.1;Integrated Security=SSPI;Persist SecurityInfo=False;Initial Catalog =Report;Data Source = " &PCName & "\WINCC"
ssql="select CurDateTime as '日期时间',T1 as '温度1',T2 as '温度2',P1 as '压力1',P2 as '压力2', F1 as '流量1'," _
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
ocom.CommandText=ssql
Set ors=ocom.Execute
Set DBGrid=ScreenItems("HFGrid")
Set DBGrid.DataSource=ors
DBGrid.Refresh
ScreenItems("HFGrid").colwidth(1)=2500
Set ors=Nothing
conn.close
Set conn=Nothing
End Sub
5.
Sub OnClick(ByVal
Item)
Dim i,j,k,m,n,filename
Dim xlapp
Dim HFGrid
Dim ors
If ScreenItems("HFGrid").rows>1 Then
End If
End Sub