原生excel汇入示例

sql DEFINE xlapp INTEGER DEFINE xlwb INTEGER MAIN DEFINE result INTEGER DEFINE str STRING --initialization of global variables LET xlapp = -1 LET xlwb = -1 --first, we must create

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
DEFINE xlapp INTEGER
DEFINE xlwb INTEGER
MAIN
  DEFINE result INTEGER
  DEFINE str STRING
--initialization of global variables
  LET xlapp = -1
  LET xlwb = -1
--first, we must create an Instance of an Excel Application
  CALL ui.Interface.frontCall("WinCOM", "CreateInstance",
   ["Excel.Application"], [xlapp])
  CALL CheckError(xlapp, __LINE__)
--then adding a Workbook to the current document
  CALL ui.interface.frontCall("WinCOM", "CallMethod",
   [xlapp, "WorkBooks.Add"], [xlwb])
  CALL CheckError(xlwb, __LINE__)
--then, setting it to be visible
  CALL ui.interface.frontCall("WinCOM", "SetProperty",
   [xlapp, "Visible", true], [result])
  CALL CheckError(result, __LINE__)
--then CALL SetProperty to set the value of the cell
  CALL ui.Interface.frontCall("WinCOM", "SetProperty", 
   [xlwb, 'activesheet.Range("A1").Value', "foo"],[result])
  CALL CheckError(result, __LINE__)
--then CALL GetProperty to check the value again
  CALL ui.Interface.frontCall("WinCOM", "GetProperty",
   [xlwb, 'activesheet.Range("A1").Value'], [str])
  CALL CheckError(str, __LINE__)
  DISPLAY "content of the cell is: " || str

  --then Free the memory on the client side
  CALL freeMemory()
END MAIN

FUNCTION freeMemory()
 DEFINE res INTEGER
  IF xlwb != -1 THEN
    CALL ui.Interface.frontCall("WinCOM","ReleaseInstance", [xlwb], [res] )
  END IF
  IF xlapp != -1 THEN
    CALL ui.Interface.frontCall("WinCOM","ReleaseInstance", [xlapp], [res] )
  END IF
END FUNCTION
FUNCTION checkError(res, lin)
  DEFINE res INTEGER
  DEFINE lin INTEGER
  DEFINE mess STRING
  IF res = -1 THEN
    DISPLAY "COM Error for call at line:", lin
    CALL ui.Interface.frontCall("WinCOM","GetError",[],[mess])
    DISPLAY mess
--let's release the memory on the GDC side
    CALL freeMemory()
    DISPLAY "Exit with COM Error."
    EXIT PROGRAM (-1)
  END IF
END FUNCTION