-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExcelCom.hs
181 lines (139 loc) · 5.4 KB
/
ExcelCom.hs
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
module ExcelCom (
module System.Win32.Com
, module System.Win32.Com.Automation
, Range, Sheet
, xlWorkbookDefault, xlSaveChanges, xlDoNotSaveChanges
, xlUp, xlDown
, getActiveCell
, getText,getFormula, getValue, getInt, getDouble, getStr, getRanges, getCells0
, setValue
, select
, showXl, hideXl
, getActiveWB, getWorkbooks, openWorkBooks
, getWSheets, getSheet, getSheetName, sheetSelect, getActiveSheet, getActiveSheet0
, setSheetName
, getRange, getCells
, createObjExl
, enumVariantsXL
)
where
import System.Win32.Com
import System.Win32.Com.Automation
import System.Win32.Com.HDirect.Pointer hiding ( freeBSTR )
import qualified System.Win32.Com.HDirect.Pointer as P ( freeBSTR )
import System.Win32.Com.HDirect.WideString
--
data Range_ a = Range__
type Range a = IDispatch (Range_ a)
data Sheet_ a = Sheet__
type Sheet a = IDispatch (Sheet_ a)
xlWorkbookDefault, xlSaveChanges, xlDoNotSaveChanges :: Int
xlDoNotSaveChanges = 2
xlSaveChanges = 1
xlWorkbookDefault = 51
xlUp, xlDown :: Int
xlDown = 0xffffefe7
xlToLeft = 0xffffefc1
xlToRight = 0xffffefbf
xlUp = 0xffffefbe
--getActiveCell :: IDispatch a -> IO (Range ())
getActiveCell :: IDispatch a -> IO (IDispatch ())
getActiveCell obj = obj # propertyGet_0 "ActiveCell"
getFormula :: IDispatch a -> IO String
getFormula obj= obj # propertyGet_0 "Formula"
getText :: IDispatch a -> IO String
getText obj= obj # propertyGet_0 "Text"
setValue :: String->IDispatch a -> IO ()
setValue str obj= obj # propertySet_1 "Value" str
getValue :: Variant b => IDispatch a -> IO b
getValue obj = obj # propertyGet_0 "Value"
getStr :: IDispatch a -> IO String
getStr = getValue
getInt :: IDispatch a -> IO Int
getInt = getValue
getDouble :: IDispatch a -> IO Double
getDouble = getValue
getRanges :: (Variant a1, Variant a2) => a1 -> a2 -> IDispatch a -> IO (IDispatch ())
getRanges cell1 cell2 =
propertyGet "Range" [ inVariant cell1 , inVariant cell2 ] outIUnknown
getCells0 :: IDispatch a0 -> IO (Range ())
getCells0 obj = obj # propertyGet "Cells" [] outIUnknown
--select :: (Variant a1)=>IDispatch () -> IO (a1)
--select = function1 "Select" [] outVariant
select :: (Variant a0)=>IDispatch a0 -> IO ()
select obj = obj # function1 "Select" [] outVariant
{-
getSelection :: IDispatch a -> IO (IDispatch ())
getSelection obj = obj # propertyGet_0 "Selection"
setText :: String->IDispatch a -> IO ()
setText str obj= obj # propertySet_1 "Text" str
-}
showXl obj = obj # propertySet "Visible" [inBool True]
hideXl obj = obj # propertySet "Visible" [inBool False]
-- WorkBooks
getActiveWB :: IDispatch a -> IO (IDispatch ())
getActiveWB obj = obj # propertyGet_0 "ActiveWorkBook"
getWorkbooks :: IDispatch a -> IO (IDispatch ())
getWorkbooks obj = obj # propertyGet_0 "Workbooks"
openWorkBooks :: String -> IDispatch a -> IO (IDispatch())
openWorkBooks fp obj = obj # propertyGet_1 "Open" fp
-- sheets
getWSheets :: IDispatch a -> IO (Sheet a)
getWSheets obj = obj # propertyGet_0 "Worksheets"
getSheet :: (Variant a0,Variant a1)=> a0 -> IDispatch a1 -> IO (IDispatch ())
getSheet n obj = obj # getWSheets ## propertyGet_1 "Item" n
sheetSelect :: (Variant a0,Variant a1) => a0 -> IDispatch a1 -> IO ()
sheetSelect n obj = obj # getSheet n ## select
--getActiveSheet0 :: IDispatch a0 -> IO (IDispatch ())
getActiveSheet0 :: IDispatch a0 -> IO (IDispatch (Sheet a))
getActiveSheet0 = propertyGet "ActiveSheet" [] outIDispatch
getActiveSheet :: IDispatch a -> IO (IDispatch (Sheet a))
getActiveSheet obj = obj # propertyGet_0 "ActiveSheet"
setSheetName :: IDispatch (Sheet a) -> String -> IO ()
setSheetName obj str = obj # propertySet_1 "Name" str
getSheetName :: IDispatch (Sheet a) -> IO String
getSheetName obj = obj # propertyGet_0 "Name"
-- Cells
--getRange :: Variant b => String -> IDispatch a -> IO b
getRange ::String -> IDispatch a -> IO (IDispatch ())
getRange rng obj = obj # propertyGet_1 "Range" rng
getCells :: Int -> Int -> IDispatch a -> IO (Range ())
getCells col row obj = obj # propertyGet_2 "Cells" col row
--
-- createObjectExcel
-- coming from Automation.hs and com.hs
--
iidAppl = mkIID "{00020400-0000-0000-C000-000000000046}"
iidIDispatch_unsafe = mkIID "{00020400-0000-0000-C000-000000000046}"
createObjExl :: IO (IDispatch ())
createObjExl = do
clsidExcel <- clsidFromProgID "Excel.Application"
pExl <- coCreateInstance clsidExcel Nothing LocalProcess iidAppl
return pExl
--
-- EnumVariant
-- coming from Automation.hs and com.hs
--
data EnumVARIANT a = EnumVARIANT
type IEnumVARIANT a = IUnknown (EnumVARIANT a)
iidIEnumVARIANT :: IID (IEnumVARIANT ())
iidIEnumVARIANT = mkIID "{00020404-0000-0000-C000-000000000046}"
newEnum :: IDispatch a -> IO (Int, IEnumVARIANT b)
newEnum ip = do
iunk <- ip # propertyGet "_NewEnum" [] outIUnknown
ienum <- iunk # queryInterface iidIEnumVARIANT
len <- ip # propertyGet "Count" [] outInt
return (len, castIface ienum)
getByOne ie def = do
mb <- catchComException (ie # enumNextOne (fromIntegral sizeofVARIANT) resVariant)
(\ _ -> return $ Just def)
case mb of
Nothing -> return []
Just x -> do
xs <- getByOne ie def
return (x:xs)
enumVariantsXL :: Variant a => a -> IDispatch b -> IO (Int, [a])
enumVariantsXL def ip = do
(len, ienum) <- newEnum ip
ls <- getByOne ienum def
return (len, ls)