过new mui.DtPicker() - 初始化DtPicker组件
通过type - 设置日历初始视图模式
通过labels - 设置默认标签区域提示语
通过beginDate和endDate设置开始,结束日期
通过customData -设置时间/日期别名
通过dtPicker.show( SelectedItemsCallback ) - 显示dtPicker
var dtpicker=new mui.DtPicker({ //注意大小写
"type": "time", // 时间格式(时分)
// "type":"datetime"(年月日时分)
// "type":"date"(年月日)
// "type":"hour"(年月日时)
"labels": ['年', 'Mon', 'Day', 'Hour', 'min'],//设置默认标签区域提示语
"beginDate": new Date(2015, 04, 25),//设置开始日期
"endDate": new Date(2016, 04, 25),//设置结束日期
"customData": {
"h": [ //时
{ value: "am", text: "上午" },
{ value: "pm", text: "下午" },
],
"i":[
{value: "10", text: "10分"},
{value: "20", text: "20分"},
]
}
})
dtpicker.show(function(items) {
/* * items.value 拼合后的 value
* items.text 拼合后的 text
* items.y 年,可以通过 rs.y.vaue 和 rs.y.text 获取值和文本
* items.m 月,用法同年
* items.d 日,用法同年
* items.h 时,用法同年
* items.i 分(minutes 的第二个字母),用法同年
*/
console.log(items.d); //得到选中的年
})
设备检点在生产过程当中是一个常态化工作,也是一个动态连续过程,通过对设备巡回检查可以提前发现问题,提前解决,也是落实安全生产的一个重要内容。
下面制作了一个检点记录的过程方法,可以实现添加、查询、删除、打印、导出等等功能,如果在工作当中合理有效地利用,可以大大提高工作效率,通过一系列统计结果,可以得出哪些设备经常出现问题,于是可以着重处理。将会尽最大限度地避免不安全因素的发生。
上图为整个操作主页,分别设置了一些功能按钮,相应地实现其功能。
本应用,没有做过多的窗体设计,只需要在此页进行即可完成其它功能,所有的处理代码实现隐藏运行,减少了对代码不熟悉的一些问题。
或以简单入手,针对不同的设置信息,可以自由设置。
所以也适合不同类型的设备检点过程。
下面看一下代码
Private Sub 查询()
On Error Resume Next
Application.DisplayAlerts=False
Application.ScreenUpdating=False
ThisWorkbook.Worksheets("检点记录").Select
Dim xArr
xArr=ActiveSheet.UsedRange
ActiveSheet.UsedRange.Rows.Hidden=False
Dim xR As Long, xc As Long, xi As Integer, ci As Integer, cc As Integer
xR=UBound(xArr, 1)
xc=UBound(xArr, 2)
ci=2 '日期列
cc=5 '单元列
For xi=3 To xR
If VBA.Format(xArr(xi, ci), "yyyy/mm/dd")=VBA.Format(Me.DTPicker1.Value, "yyyy/mm/dd") And _
Me.ComboBox1.Value=xArr(xi, cc) Then
ActiveSheet.Rows(xi).Hidden=False'隐藏行
Else
ActiveSheet.Rows(xi).Hidden=True'显示行
End If
Next xi
Erase xArr
Application.DisplayAlerts=True
Application.ScreenUpdating=True
End Sub
查询过程通过一个简单的隐藏功能来实现,如果查询到信息即显示在表格中,不符合查询条件就隐藏掉,代码简单,理论也简单没有过多深度理解的内容。
Private Sub SaveFiles() '导出文件
On Error Resume Next
Dim xSheetName As String
Dim isTrue As Integer
xSheetName=VBA.InputBox("输入文件名...", "导出文件", VBA.Format(VBA.Date, "yyyymmdd"))
If VBA.Len(VBA.Trim(xSheetName))=0 Then Exit Sub
If VBA.Dir(ThisWorkbook.Path & "\" & xSheetName & ".xlsx") <> "" Then
isTrue=MsgBox("文件已经存在!是否要覆盖?", vbYesNo, "提示")
If isTrue <> 6 Then Exit Sub
End If
Application.DisplayAlerts=False
Application.ScreenUpdating=False
Application.CutCopyMode=False
ActiveSheet.Cells(2, 1).Resize(ActiveSheet.UsedRange.Rows.Count - 1, ActiveSheet.UsedRange.Columns.Count).Copy
Dim xExcel As Excel.Application
Dim xBook As Workbook
Dim xSheet As Worksheet
Set xExcel=CreateObject("Excel.Application")'新建应用
Set xBook=xExcel.Workbooks.Add
Set xSheet=xBook.Worksheets(1)
xSheet.Cells(1, 1).PasteSpecial xlPasteAll
xSheet.Name=xSheetName
xExcel.Visible=True
xBook.SaveAs ThisWorkbook.Path & "\" & xSheetName & ".xlsx"
xBook.Close
xExcel.Quit
If Err.Number=0 Then
MsgBox "文件导出成功!" & VBA.vbCrLf & xSheetName, vbInformation, "提示"
End If
Application.DisplayAlerts=True
Application.ScreenUpdating=True
Application.CutCopyMode=True
End Sub
文件导出功能是很实用的功能,可以十分方便地将当前数据备份到新工作表中,为此做了一些代码处理,而且此代码也可以直接使用,有兴趣可以复制应用一下。
此应用还有一些未完善的功能,比如统计功能,经过一些数据统计可得到一些想要的结果,如有必要下一步继续完成一下。
欢迎关注、收藏
---END---
】原理简述
在第1课中已经阐述了如何将PLC的数据通过OPC(kepeare)如何定时写入mssql数据库中,日报表是通过数据库查询语句将MSSQL表的内容按照时间查询并显示到flexcell表格中。主要的设计流程:
(1)建立数据库的连接
(2)利用数据库的查询语句,按照选择日期时间,查询数据。
【二】基础功能设计
2.1 界面设计
几大控件
(1)时间控件DTpicker
(2) 按钮控件 commandbutton
(3)通讯CommonDialog
(4)表格控件Grid
查询代码:
打印代码
初始化代码
数据导出代码
'以下为链接数据库SQL
Const g_conString=" Provider=MSDASQL.1;Persist Security Info=False;Data Source=SQL_KEP;Initial Catalog=SQL_KEP" '数据库连接代码
Dim g_value(100) As String
Private Sub Command1_Click() '导出到Excel文件
ss=CommonDialog1.FileName
CommonDialog1.ShowSave
If ss <> CommonDialog1.FileName Then
Grid1.ExportToExcel (CommonDialog1.FileName + ".xls")
End If
End Sub
Private Sub Command2_Click() '查询按钮
Dim strsql
Dim cn As ADODB.Connection
Dim record As ADODB.Recordset
Set cn=New ADODB.Connection
cn.Open g_conString
Set record=New ADODB.Recordset
st=Str(d_start_date.Value)
If Len(st) < 14 Then '
st=st + " 00:00:00"
End If
st1=Str(d_end_date.Value)
If Len(st1) < 14 Then '
st1=st1 + " 000:00:00"
End If
strsql="select tt,t1,t2,t3,t4,t5 from tt1 where TT>='" + st + "' and TT<='" + st1 + "'order by xh" '此处只查询了5个数据
'查询语句 从SQL的tt1表格中 查询 tt,t1,t2,t3,t4,t5 数据, 时间介于
record.Open strsql, cn
j=2 '目录列
Grid1.Rows=1
If record.EOF=True Then
record.Close
Set record=Nothing
cn.Close
Set cn=Nothing
Exit Sub
End If
Dim maxv(5) As Single
Dim minv(5) As Single
Dim avg(5) As Single
For i=0 To 4
maxv(i)=-10000
minv(i)=10000
avg(i)=0
Dim cxv As Single
Next i
Dim num As Integer
num=0
Do While record.EOF=False
Grid1.Rows=j + 4
Grid1.Cell(j, 0).Text=Str(j)
st3=Str(CDate(record.Fields(0)))
If Len(st3) < 14 Then
st3=st3 + " 00:00:00"
End If
Grid1.Cell(j, 1).Text=st3
For i=1 To 5
num=num + 1
If record.Fields(i) > -10000 Then
cxv=record.Fields(i)
Else
cxv=0
End If
If i=1 Then
'增加一行可以否则EXCEL导出没有名称)
Grid1.Cell(1, 1).Text="日期时间"
Grid1.Cell(1, 2).Text="取水液位"
Grid1.Cell(1, 3).Text="进水流量"
Grid1.Cell(1, 4).Text="进水浊度"
Grid1.Cell(1, 5).Text="1#储药罐液位"
Grid1.Cell(1, 6).Text="2#储药罐液位"
Grid1.Cell(j, i + 1).Text=Format(cxv, "0.000") + " 米" '数据标准化及单位
End If
If i=2 Then
Grid1.Cell(j, i + 1).Text=Format(cxv, "0") + " m3/h"
End If
If i=3 Then
Grid1.Cell(j, i + 1).Text=Format(cxv, "0.00") + " NTU"
End If
If i=4 Then
Grid1.Cell(j, i + 1).Text=Format(cxv, "0.000") + " 米"
End If
If i=5 Then
Grid1.Cell(j, i + 1).Text=Format(cxv, "0.000") + " 米"
End If
If maxv(i - 1) < cxv Then
maxv(i - 1)=cxv
End If
If minv(i - 1) > cxv Then
minv(i - 1)=cxv
End If
avg(i - 1)=avg(i - 1) + cxv
Next i
'num=num + 1
j=j + 1
record.MoveNext
Loop
Grid1.Cell(j, 1).Text="平均值"
Grid1.Cell(j + 1, 1).Text="最大值"
Grid1.Cell(j + 2, 1).Text="最小值"
For i=0 To 4
If i=0 Then
Grid1.Cell(j, i + 2).Text=Format(avg(i) / num, "0.000") + " 米"
Grid1.Cell(j + 1, i + 2).Text=Format(maxv(i), "0.000") + " 米"
Grid1.Cell(j + 2, i + 2).Text=Format(minv(i), "0.000") + " 米"
End If
If i=1 Then
Grid1.Cell(j, i + 2).Text=Format(avg(i) / num, "0") + " m3/h"
Grid1.Cell(j + 1, i + 2).Text=Format(maxv(i), "0") + " m3/h"
Grid1.Cell(j + 2, i + 2).Text=Format(minv(i), "0") + " m3/h"
End If
If i=2 Then
Grid1.Cell(j, i + 2).Text=Format(avg(i) / num, "0.00") + " NTU"
Grid1.Cell(j + 1, i + 2).Text=Format(maxv(i), "0.00") + " NTU"
Grid1.Cell(j + 2, i + 2).Text=Format(minv(i), "0.00") + " NTU"
End If
If i=3 Then
Grid1.Cell(j, i + 2).Text=Format(avg(i) / num, "0.000") + " 米"
Grid1.Cell(j + 1, i + 2).Text=Format(maxv(i), "0.000") + " 米"
Grid1.Cell(j + 2, i + 2).Text=Format(minv(i), "0.000") + " 米"
End If
If i=4 Then
Grid1.Cell(j, i + 2).Text=Format(avg(i) / num, "0.000") + " 米"
Grid1.Cell(j + 1, i + 2).Text=Format(maxv(i), "0.000") + " 米"
Grid1.Cell(j + 2, i + 2).Text=Format(minv(i), "0.000") + " 米"
End If
Next i
record.Close
Set record=Nothing
cn.Close
Set cn=Nothing
End Sub
Private Sub Command3_Click()
Grid1.PrintPreview '打印预览
End Sub
Private Sub Form_Initialize()
Grid1.SetRegisterInformation "CNwinndy", "W]vyY-nonvk-u\nty-Zbl_e-`hms^" '注册flexcell表格控件
Grid1.PageSetup.Orientation=cellLandscape
Grid1.Cols=7
'以下为设置表格第一行的名称
Grid1.Cell(0, 1).Text="日期时间"
Grid1.Cell(0, 2).Text="取水液位"
Grid1.Cell(0, 3).Text="进水流量"
Grid1.Cell(0, 4).Text="进水浊度"
Grid1.Cell(0, 5).Text="1#储药罐液位"
Grid1.Cell(0, 6).Text="2#储药罐液位"
'以下为设置表格宽度
Grid1.Column(1).Width=130
Grid1.Column(2).Width=100
Grid1.Column(3).Width=100
Grid1.Column(4).Width=100
Grid1.Column(5).Width=100
Grid1.Column(6).Width=100
Grid1.PageSetup.PrintFixedRow=True
Grid1.PageSetup.PrintGridlines=True
d_start_date.Value=Now
d_end_date.Value=Now
End Sub
Private Sub Grid1_AfterReorderColumn(ByVal OriginalPosition As Long, ByVal NewPosition As Long)
Grid1.SetRegisterInformation "CNwinndy", "W]vyY-nonvk-u\nty-Zbl_e-`hms^"
End Sub
以上为日报表的源代码
其他数据与此相同
In_data1,in_data2,in_data3都代码相似
*请认真填写需求信息,我们会在24小时内与您取得联系。