恭喜,你发布的帖子
发布于 2019-02-13 19:44:55
9楼
我说句闲话,成版的帖子对于楼主来说一点用都没……看着问题的方式就是想吃个现成的。可惜这个东西没有现成的可以直接用。
Dim m,i
Dim T1,T2
T1=ScreenItems("T1").value
T2=ScreenItems("T2").value
Dim Year1,Month1,Day1
Year1=Year(T1)
month1=Month(T1)
day1=Day(T1)
T1=""&year1&"-"&month1&"-"&day1&""
Year1=Year(T2)
month1=Month(T2)
day1=Day(T2)
T2=""&year1&"-"&month1&"-"&day1&""
'以上代码看不懂 可以参考教材第35页内容
Dim scon,conn
sCon="Driver={SQL Server};Server=DONG-PC\WINCC;database=aa;UID=;PWD=;"
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = sCon
conn.CursorLocation = 3
conn.Open
'数据库连接,参考教材第70页上半部以及71页下半部
'若要检测数据库是否连接成功,可以用msgbox conn.state,70页connection常用属性5
Dim ssql1
sSql1="select ID,convert(char(20),日期时间,120) 日期时间,进口温度,进口压力,出口温度,出口压力,储气罐温度,储气罐压力,气体流量 from BStable where 日期时间 between'"&T1&" 00:00:00' and '"&T2&" 23:59:59'"&vbCrLf&_
"union all"&vbCrLf&_
"Select ''ID,N'总量'日期时间,sum(进口温度)进口温度,sum(进口压力)进口压力,sum(出口温度)出口温度,sum(出口压力)出口压力,sum(储气罐温度)储气罐温度,sum(储气罐压力)储气罐压力,sum(气体流量)气体流量 from BStable where 日期时间 between'"&T1&" 00:00:00' and '"&T2&" 23:59:59'"&vbCrLf&_
"union all"&vbCrLf&_
"Select ''ID,N'平均值'日期时间,round(avg(进口温度),2)进口温度,round(avg(进口压力),2)进口压力,round(avg(出口温度),2)出口温度,round(avg(出口压力),2)出口压力,round(avg(储气罐温度),2)储气罐温度,round(avg(储气罐压力),2)储气罐压力,round(avg(气体流量),2)气体流量 from BStable where 日期时间 between'"&T1&" 00:00:00' and '"&T2&" 23:59:59'"
'SQL查询语句,参考教材第60页。
Dim ocom,ors1
Set oRs1 = CreateObject("ADODB.Recordset")
Set oCom = CreateObject("ADODB.Command")
oCom.CommandType = 1
Set oCom.ActiveConnection = conn
oCom.CommandText = sSql1
Set oRs1 = oCom.Execute
m = oRs1.RecordCount
'以上脚本请参考教材72至76页。有详细的对象属性方法介绍。
'填充数据到view中
Dim oList,rownum
If (m > 0) Then
oRs1.MoveFirst
Set oList = ScreenItems("view1")'VIEW1和之前的T1\T2均为画面中的控件名称,可改。
'OList属性设置参考附录一
oList.clear
oList.Rows=2
olist.Cols=2
oList.fixedRows=1
olist.fixedCols=1
rownum=1
oList.Rows=m+3
olist.Cols=ors1.fields.count
For i = 0 To 8
olist.ColAlignment(i)=4
Next
oList.ColWidth(0) = 800
oList.ColWidth(1) = 2000
oList.ColWidth(2) = 1400
oList.ColWidth(3) = 1400
oList.ColWidth(4) = 1400
oList.ColWidth(5) = 1400
oList.ColWidth(6) = 1400
oList.ColWidth(7) = 1400
oList.ColWidth(8) = 1400
olist.TextMatrix(0,0)=ors1.fields(0).name
olist.TextMatrix(0,1)=ors1.fields(1).name
olist.TextMatrix(0,2)=ors1.fields(2).name
olist.TextMatrix(0,3)=ors1.fields(3).name
olist.TextMatrix(0,4)=ors1.fields(4).name
olist.TextMatrix(0,5)=ors1.fields(5).name
olist.TextMatrix(0,6)=ors1.fields(6).name
olist.TextMatrix(0,7)=ors1.fields(7).name
olist.TextMatrix(0,8)=ors1.fields(8).name
Do While Not oRs1.EOF
rownum=rownum+1
olist.TextMatrix(rownum-1,0)=ors1.fields(0).value
olist.TextMatrix(rownum-1,1)=ors1.fields(1).value
olist.TextMatrix(rownum-1,2)=Round(ors1.fields(2).value,2)
olist.TextMatrix(rownum-1,3)=Round(ors1.fields(3).value,2)
olist.TextMatrix(rownum-1,4)=Round(ors1.fields(4).value,2)
olist.TextMatrix(rownum-1,5)=Round(ors1.fields(5).value,2)
olist.TextMatrix(rownum-1,6)=Round(ors1.fields(6).value,2)
olist.TextMatrix(rownum-1,7)=Round(ors1.fields(7).value,2)
olist.TextMatrix(rownum-1,8)=Round(ors1.fields(8).value,2)
oRs1.MoveNext
Loop
oRs1.Close
Else
MsgBox "没有所需数据"
End If
Set oRs = Nothing
Set oRs1 = Nothing
conn.Close
Set conn = Nothing
Dim m,i,j,n
Dim objExcelApp,objExcelBook,objExcelSheet
'打开Excel模板
Set objExcelApp = CreateObject("Excel.Application")
objExcelApp.Visible = True
objExcelApp.Workbooks.Open "D:\report1\report.xls"
objExcelApp.Worksheets("sheet1").Activate
'打开Excel模板
Dim olist
Set olist=ScreenItems("view1")
m=olist.Cols
i=olist.Rows
For j = 1 To m
For n = 1 To i
objExcelApp.Worksheets("Sheet1").cells(n,j).value= olist.TextMatrix (n-1,j-1)
Next
Next
Dim patch,filename
filename=CStr(Year(Now))&CStr(Month(Now))&CStr(Day(Now))&CStr(Hour(Now))+CStr(Minute(Now))&CStr(Second(Now))
patch= "D:\report1\report\"&filename&".xls"
objExcelApp.ActiveWorkbook.SaveAs patch
' objExcelApp.Workbooks.Close
' objExcelApp.Quit
Set objExcelApp= Nothing
' MsgBox "成功生成数据文件!"
item.Enabled = True
我贴两段脚本楼主自己改改吧 真的没有现成的东西可以用
请问贴中所指参考教材是什么教材
请填写推广理由:
分享
只看
楼主