11、在 iFIX 中怎样生成包含历史数据的 Excel 电子表格?
2004-10-10 00:30阅读:
这样的功能以及其他的功能均可在 iFIX 中方便地通过 VBA代码、Scheduler和历史数据 ODBC 驱动程序来完成。
以下是答案,下列代码是一个示例,此代码根据 Scheduler 中的定时器每隔 24 小时执行一次,它会提取前 24
小时的历史数据,写入一个电子表格,然后使用当前的日期作为文件名存盘。
注意:此例子只提供给 Excel97,如用Excel2000,方法会有不同。
步骤:
1) 在 iFIX Workspace 中生成或打开一个 Schedule。
2)
加入一个基于时间的事件,每隔24小时执行一次,执行时间可根据需要而定,如你可能希望在每天的午夜打印报表。
3) 点击 VB Editor 按钮,进入 Scheduler 中的代码编辑。
4) 转到 Tools/References 菜单下,选择Microsoft Excel 8.0 Object
Library (选取选项框)。
5) 在 sub 和 end sub 之间粘贴如下代码。
On Error Resume Next
燑/P> 燑/P> 'This
Section
performs the date calculations and formats them for SQL Statement
dteCurrent = Now 'Captures
Current Date/Time
'The variable dblHoursPrev
represents the number of hours you want to subtract
'from the current date. By
changing this number you can manipulate the amount
'of data that is stored to the
file. If you change number you also have to modify
'the trigger time of your
schedule and the filename this code saves the Excel file
with.
dblHoursPrev = -24
dteCalcStart = DateAdd('h',
dblHoursPrev, dteCurrent) 'Performs actual date
calculation
'Formats end date to be in
correct format for SQL query
strEndToday =
Format(dteCurrent, 'YYYY-MM-DD HH:MM:SS')
'Formats start date to be in
correct format for SQL query
strStartToday =
Format(dteCalcStart, 'YYYY-MM-DD HH:MM:SS')
'This section creates the Excel
object and runs the query using Excel's query engine
Set objExcel =
CreateObject('excel.application')
Dim rngUsedRange As
Range
Dim strNewRange As
String
Dim sngNewRow As
Single
objExcel.Workbooks.Add
Set rngUsedRange =
objExcel.ActiveWorkbook.ActiveSheet.UsedRange
sngNewRow =
objExcel.ActiveWorkbook.ActiveSheet.UsedRange.Rows.Count +
2
If sngNewRow = 3
Then
strNewRange = 'A1'
Else
strNewRange = 'A' &
sngNewRow
End If
With
objExcel.ActiveWorkbook.ActiveSheet.QueryTables.Add(Connection:=
_
'ODBC;DSN=FIX Dynamics
Historical Data;DBQ=;SERVER=NotTheServer', Destination
_
:=objExcel.Range(strNewRange))
'Change THISNODE below to the
Node Name from which you want to retrive the data.
'Below is the actual SQL
statment Excel will run.