1. 程式人生 > >intouch把查詢到的歷史報警記錄寫到excel

intouch把查詢到的歷史報警記錄寫到excel

1.建立一個數據源SQL_Date,資料來源的配置如下

2.建立一個繫結表。只要用到sqlconnect去連線資料庫,就必須先建立繫結表。







3.寫一個quickfunction,名字叫SQLTOEXCEL,內容如下


DIM Counter AS INTEGER;
DIM Cell AS MESSAGE;
DIM Result AS INTEGER;
DIM TotalCount AS INTEGER;
DIM WhereExpr AS MESSAGE;

{連線資料庫}
Result=SQLConnect(GMS_SQL_ConnectionId,  "DSN=SQL_Date;UID=sa;PWD=123456"  ); 
 LogMessage("SQLConnect 返回值:" + StringFromIntg (Result,10));

{查詢條件}
WhereExpr= "Area like '%" + FMCS_ALARM_GROUP + "%'"   + " And EventStamp >= '" +FMCS_DATETIME0+ "'"  + " And EventStamp <= '" +FMCS_DATETIME1+ "'"   ;
 LogMessage("SQLSelect 的條件是:" + WhereExpr);

{查詢}
Result = SQLSelect( GMS_SQL_ConnectionId, "v_AlarmHistory", "abtag", WhereExpr, "" );
TotalCount=SQLNumRows(GMS_SQL_ConnectionId);
 LogMessage("共計有記錄數 :" + StringFromIntg (TotalCount,10));

  IF(  TotalCount  == 0 ) THEN
                    RETURN  -1 ;
ENDIF;

{用wwpoke函式寫到表格裡面}
Cell = "r1c1";
WWPoke( "excel", "sheet1", Cell, "時間" );
Cell = "r1c2";
WWPoke( "excel", "sheet1", Cell, "狀態" );
Cell = "r1c3";
WWPoke( "excel", "sheet1", Cell, "名稱" );
Cell = "r1c4";
WWPoke( "excel", "sheet1", Cell, "值" );
Cell = "r1c5";
WWPoke( "excel", "sheet1", Cell, "報警限" );
Cell = "r1c6";
WWPoke( "excel", "sheet1", Cell, "報警註釋" );
Cell = "r1c7";
WWPoke( "excel", "sheet1", Cell, "組" );
Cell = "r1c8";
WWPoke( "excel", "sheet1", Cell, "操作員" );
Cell = "r1c9";
WWPoke( "excel", "sheet1", Cell, "操作員節點" );
Cell = "r1c10";
WWPoke( "excel", "sheet1", Cell, "持續時間" );

{獲取第一條記錄}
Result = SQLFirst( GMS_SQL_ConnectionId );
  IF( Result < 0 ) THEN
               LogMessage("SQLFirst 執行返回值 :" + StringFromIntg (Result,10));    
                    RETURN  -1 ;
ENDIF;


IF ( CALL WRITETOEXCEL(2) == -1 ) THEN 
    RETURN -1;
ENDIF;


FOR Counter = 3 TO TotalCount+1

{遍歷所有記錄}
Result= SQLNext( GMS_SQL_ConnectionId );

IF( Result == -5 ) THEN
    EXIT FOR;
ENDIF;


IF ( CALL WRITETOEXCEL(Counter) == -1 ) THEN 
    EXIT FOR;
ENDIF;


NEXT;

{關閉資料庫}
SQLDisconnect(GMS_SQL_ConnectionId); 






4.WRITETOEXCEL子函式的內容如下
 

DIM ROW AS MESSAGE;
DIM Result AS INTEGER;

Row = StringFromIntg( iRow, 10 );


GMS_SQL_Cell    = "r" + Row + "c1";
LogMessage("正在寫入"+GMS_SQL_Cell);
Result    = WWPoke( "excel", "sheet1", GMS_SQL_Cell, GMS_SQL_EventStamp );
IF( Result == -1 ) THEN
LogMessage(GMS_SQL_Cell+" 資料未成功寫入。可能的原因是應用程式未在執行,或是主題或專案不存在。");
 RETURN -1;
ENDIF;
IF( Result == 0 )  THEN
LogMessage(GMS_SQL_Cell+" 應用程式正忙,導致資料未成功寫入。");
 RETURN -1;
ENDIF;

GMS_SQL_Cell    = "r" + Row + "c2";
LogMessage("正在寫入"+GMS_SQL_Cell);
Result    = WWPoke( "excel", "sheet1", GMS_SQL_Cell, GMS_SQL_AlarmState );
IF( Result == -1 ) THEN
LogMessage(GMS_SQL_Cell+" 資料未成功寫入。可能的原因是應用程式未在執行,或是主題或專案不存在。");
 RETURN -1;
ENDIF;
IF( Result == 0 ) THEN
LogMessage(GMS_SQL_Cell+" 應用程式正忙,導致資料未成功寫入。");
 RETURN -1;
ENDIF;

GMS_SQL_Cell    = "r" + Row + "c3";
LogMessage("正在寫入"+GMS_SQL_Cell);
Result    = WWPoke( "excel", "sheet1", GMS_SQL_Cell, GMS_SQL_TagName );
IF( Result == -1 ) THEN
LogMessage(GMS_SQL_Cell+" 資料未成功寫入。可能的原因是應用程式未在執行,或是主題或專案不存在。");
 RETURN -1;
ENDIF;
IF( Result == 0 ) THEN
LogMessage(GMS_SQL_Cell+" 應用程式正忙,導致資料未成功寫入。");
 RETURN -1;
ENDIF;

GMS_SQL_Cell    = "r" + Row + "c4";
LogMessage("正在寫入"+GMS_SQL_Cell);
Result    = WWPoke( "excel", "sheet1", GMS_SQL_Cell, GMS_SQL_Value );
IF( Result == -1 ) THEN
LogMessage(GMS_SQL_Cell+" 資料未成功寫入。可能的原因是應用程式未在執行,或是主題或專案不存在。");
 RETURN -1;
ENDIF;
IF( Result == 0 ) THEN
LogMessage(GMS_SQL_Cell+" 應用程式正忙,導致資料未成功寫入。");
 RETURN -1;
ENDIF;

GMS_SQL_Cell    = "r" + Row + "c5";
LogMessage("正在寫入"+GMS_SQL_Cell);
Result    = WWPoke( "excel", "sheet1", GMS_SQL_Cell, GMS_SQL_CheckValue );
IF( Result == -1 ) THEN
LogMessage(GMS_SQL_Cell+" 資料未成功寫入。可能的原因是應用程式未在執行,或是主題或專案不存在。");
 RETURN -1;
ENDIF;
IF( Result == 0 ) THEN
LogMessage(GMS_SQL_Cell+" 應用程式正忙,導致資料未成功寫入。");
 RETURN -1;
ENDIF;

GMS_SQL_Cell    = "r" + Row + "c6";
LogMessage("正在寫入"+GMS_SQL_Cell);
Result    = WWPoke( "excel", "sheet1", GMS_SQL_Cell, GMS_SQL_Description );
IF( Result == -1 ) THEN
LogMessage(GMS_SQL_Cell+" 資料未成功寫入。可能的原因是應用程式未在執行,或是主題或專案不存在。");
 RETURN -1;
ENDIF;
IF( Result == 0 ) THEN
LogMessage(GMS_SQL_Cell+" 應用程式正忙,導致資料未成功寫入。");
 RETURN -1;
ENDIF;


GMS_SQL_Cell    = "r" + Row + "c7";
LogMessage("正在寫入"+GMS_SQL_Cell);
Result    = WWPoke( "excel", "sheet1", GMS_SQL_Cell, GMS_SQL_Area );
IF( Result == -1 ) THEN
LogMessage(GMS_SQL_Cell+" 資料未成功寫入。可能的原因是應用程式未在執行,或是主題或專案不存在。");
 RETURN -1;
ENDIF;
IF( Result == 0 ) THEN
LogMessage(GMS_SQL_Cell+" 應用程式正忙,導致資料未成功寫入。");
 RETURN -1;
ENDIF;

GMS_SQL_Cell    = "r" + Row + "c8";
LogMessage("正在寫入"+GMS_SQL_Cell);
Result    = WWPoke( "excel", "sheet1", GMS_SQL_Cell, "" );
IF( Result == -1 ) THEN
LogMessage(GMS_SQL_Cell+" 資料未成功寫入。可能的原因是應用程式未在執行,或是主題或專案不存在。");
 RETURN -1;
ENDIF;
IF( Result == 0 ) THEN
LogMessage(GMS_SQL_Cell+" 應用程式正忙,導致資料未成功寫入。");
 RETURN -1;
ENDIF;

GMS_SQL_Cell    = "r" + Row + "c9";
LogMessage("正在寫入"+GMS_SQL_Cell);
Result    = WWPoke( "excel", "sheet1", GMS_SQL_Cell, "" );
IF( Result == -1 ) THEN
LogMessage(GMS_SQL_Cell+" 資料未成功寫入。可能的原因是應用程式未在執行,或是主題或專案不存在。");
 RETURN -1;
ENDIF;
IF( Result == 0 ) THEN
LogMessage(GMS_SQL_Cell+" 應用程式正忙,導致資料未成功寫入。");
 RETURN -1;
ENDIF;
 

5.測試
在呼叫SQLTOEXCEL之前需要先新建一個excel表格,用微軟的office開啟(用wps開啟無效),路徑和名字任意,其他都預設
然後在一個按鈕的腳本里面去呼叫CALL SQLTOEXCEL()即可
另外由於wwpoke函式執行較慢,導致intouch的for迴圈的預設5秒鐘超時時間會觸發,所以需要改大一些比如改成5分鐘:修改專案目錄下INTOUCH.ini檔案的LoopTimeout=300

測試日誌如下