实现IDL对excel对图表的操作,因为对IDL编程不熟悉(没有学过,看过几行代码后写过IDL代码而已,任何语言感觉差不多,学了c/c++ 其他的语言都是浮云)。
1 ;-----------------------------------------------------------------
2 ; GetAllDataFromCurrentWorksheet Method. Returns all valid, contiguous data
3 ; stored
in currently selected worksheet
4 ;
5 ; Example:
6 ;
7 ; oIDL_Excel = obj_new("ITCffExcel", fileName, SHEET='irreg_grid1')
8 ; data = oIDL_Excel->GetAllDataFromCurrentWorksheet()
9 ;-
10 FUNCTION ITCffExcel::GetAllDataFromCurrentWorksheet
11 12 COMPILE_OPT IDL2
13 14 On_Error, 2
15 16 IF (Obj_Valid(self.oCellRange)) THEN Obj_Destroy, self.oCellRange
17 self.oSheet->GetProperty, Usedrange = oUsedCells
18 self.oCellRange = oUsedCells
19 self.oCellRange->GetProperty, Value = myValues
20 IF (Size(myValues, /Type) EQ 0) THEN BEGIN ; undefined
21 Message, 'Invalid Range '+range+'? Mixed data types not supported.'+ $
22 ' Data must be numeric and contiguous.', /Ioerror
23 ENDIF ELSE BEGIN
24 ; Rearrange into IDL column-major format so user doesn't have to
25 dims = Size(myValues, /Dimensions)
26 myValues = Transpose(Reform(Temporary(myValues), dims[1], dims[0]))
27 28 Return, myValues
29 ENDELSE
30 END
31 32 33 pro ITCffExcel::SetChart,inputRange
34 35 ; self.oSheet->GetProperty,Shapes = MyShapes
36 ;MyShapes->AddChart
37 38 self.oExcel->GetProperty,Charts = MyChart
39 MyChart->add
40 ;获得刚刚添加的表,对它进行设置
41 self.oExcel->GetProperty,ActiveChart = MyActiveChart
42 ;又要获取ChartType 进行赋值
43 MyActiveChart->GetProperty,ChartType = MyChartType
44 MyChartType = 51 ;这个值我是通过看C#代码跟进去看到 。不想获取对象来获取
45 ;设置数据源
46 IF (Obj_Valid(self.oCellRange)) THEN Obj_Destroy, self.oCellRange
47 48 IF (Size(inputRange, /Dimensions) EQ 2) THEN BEGIN
49 self.oSheet->GetProperty, inputRange[0], inputRange[1], Range = oCellRange
50 self.oCellRange = oCellRange
51 ENDIF ELSE BEGIN
52 self.oSheet->GetProperty, inputRange, Range = oCellRange
53 self.oCellRange = oCellRange
54 ENDELSE
55 MyActiveChart->SetSourceData,self.oCellRange,2
56 MyActiveChart->Location,2,"sheet1"
57 58 END
59 60 ;;;设置图表标题
61 pro ITCffExcel::SetChartTitle,strTitle
62 63 MyChart = self.oSheet->ChartObjects("Chart 1")
64 MyChart->Activate
65 self.oExcel->GetProperty,ActiveChart = MyActiveChart
66 MyActiveChart->GetProperty,ChartTitle = ChatTitle
67 ChatTitle->SetProperty,Text = strTitle
68 69 70 71 72 73 END
74 ;-----------------------------------------------------------------
75 ;
76 ; 删除指定表格。这样就可以就填充了。
77 ;
78 pro ITCffExcel::DeleteChart,inputRange
79 IF (Obj_Valid(self.oCellRange)) THEN Obj_Destroy, self.oCellRange
80 81 IF (Size(inputRange, /Dimensions) EQ 2) THEN BEGIN
82 self.oSheet->GetProperty, inputRange[0], inputRange[1], Range = oCellRange
83 self.oCellRange = oCellRange
84 ENDIF ELSE BEGIN
85 self.oSheet->GetProperty, inputRange, Range = oCellRange
86 self.oCellRange = oCellRange
87 ENDELSE
88 89 ;进行删除
90 ;self.oCellRange->GetProperty,Select = MySelect
91 ObjSelect = self.oCellRange->Select()
92 ;ObjSelect->Delete
93 self.oCellRange->Delete
94 95 ;MyActiveChart->SetSourceData,self.oCellRange,2
96 ;MyActiveChart->Location,2,"sheet1"
97 98 END
99 100 ;;设置y轴的属性
101 ;XorY : 0 代表设置 x , 1 代表设置y
102 ;MaximumScale:最大值
103 ;MinimumScale: 最小值
104 ;MinorUnit: 最小刻度
105 ;self.oSheet
106 PRO ITCffExcel::SetXYAxes, XorY,nMaximumScale,nMinimumScale,nMajorUnit
107 MyChart = self.oSheet->ChartObjects("Chart 1")
108 MyChart->Activate
109 110 self.oExcel->GetProperty,ActiveChart = MyActiveChart
111 if (XorY EQ 0)then begin
112 Axis = MyActiveChart->Axes(1,1)
113 endif
else begin
114 Axis = MyActiveChart->Axes(2,1)
115 endelse
116 ;Axis->GetProperty,MaximumScale = MyMaximumScale
117 Axis->SetProperty,MaximumScale = nMaximumScale
118 Axis->SetProperty,MinimumScale = nMinimumScale
119 Axis->SetProperty,MajorUnit = nMajorUnit
120 121 End
122 123 124 125 ;-----------------------------------------------------------------
126 ;
127 ;保存文件
128 ;
129 PRO ITCffExcel::Save, fileName
130 131 COMPILE_OPT IDL2
132 133 IF N_Elements(fileName) EQ 0 THEN fileName = self.fileName
134 135 self.oWkBookData->SaveAs, fileName
136 END
137 138 ;-----------------------------------------------------------------
139 ; GetData Method. Data from passed
in range parameter
140 ;
141 ; @param inputRange {
in}{required}{scalar or 2 element
string array - 1 and
142 ; 2D ranges supported} input
143 ;
144 ; Example:
145 ;
146 ; oExcel = Obj_New("ITCffExcel", "myData.xls")
147 ; myData= oExcel->GetData("A1:A100" | ["P1:P100","S1:S100"])
148 ;-
149 FUNCTION ITCffExcel::GetRangeData, inputRange
150 151 COMPILE_OPT IDL2
152 153 On_Error, 2
154 155 IF (Obj_Valid(self.oCellRange)) THEN Obj_Destroy, self.oCellRange
156 157 IF (Size(inputRange, /Dimensions) EQ 2) THEN BEGIN
158 self.oSheet->GetProperty, inputRange[0], inputRange[1], Range = oCellRange
159 self.oCellRange = oCellRange
160 ENDIF ELSE BEGIN
161 self.oSheet->GetProperty, inputRange, Range = oCellRange
162 self.oCellRange = oCellRange
163 ENDELSE
164 165 self.oCellRange->GetProperty, Value = myValues
166 167 IF (Size(myValues, /Type) EQ 0) THEN BEGIN ; undefined
168 Message, 'Invalid Range '+range+'? Mixed data types not supported.'+ $
169 ' Data must be numeric and contiguous.', /Ioerror
170 ENDIF ELSE Return, myValues
171 END
172 173 ;-----------------------------------------------------------------
174 ;
175 ;设置单元格数据
176 ;
177 PRO ITCffExcel::SetRangeData, inputRange, value
178 179 COMPILE_OPT IDL2
180 181 On_Error, 2
182 183 IF (Obj_Valid(self.oCellRange)) THEN Obj_Destroy, self.oCellRange
184 185 IF (Size(inputRange, /Dimensions) EQ 2) THEN BEGIN
186 self.oSheet->GetProperty, inputRange[0], inputRange[1], Range = oCellRange
187 self.oCellRange = oCellRange
188 ENDIF ELSE BEGIN
189 self.oSheet->GetProperty, inputRange, Range = oCellRange
190 self.oCellRange = oCellRange
191 ENDELSE
192 193 self.oCellRange->SetProperty, 'select', Value = value
194 195 Obj_Destroy, self.oCellRange
196 END
197 198 ;-----------------------------------------------------------------
199 ; Sets Properties on ITCffExcel Object
200 ;
201 ; @Keyword:
202 ;
203 ; Sheet {
in}{optional}{scalar
string} input
204 ;
205 ; Example:
206 ;
207 ; oExcel = Obj_New("ITCffExcel", "myData.xls")
208 ; oExcel->SetPropery, SHEET="Sheet2"
209 ;-
210 PRO ITCffExcel::SetProperty, Sheet = sheet, _Extra = extra
211 212 COMPILE_OPT IDL2
213 214 On_Error, 2
215 216 IF (Obj_Valid(self.oSheet)) THEN Obj_Destroy, self.oSheet
217 218 self.oSheetCollection->GetProperty, sheet, Item = oSheet
219 220 self.oSheet = oSheet
221 END
222 223 ;-----------------------------------------------------------------
224 ;
225 ;析构
226 ;
227 PRO ITCffExcel::Cleanup
228 229 COMPILE_OPT IDL2
230 231 On_Error, 2
232 233 self.oWkBookData->SetProperty, Saved = 1
234 self.oWkBookCollection->Close
235 self.oExcel->Quit
236 237 Obj_Destroy, [self.oSheet, self.oCellRange, self.oSheetCollection, $
238 self.oWkBookData, self.oWkBookCollection, self.oExcel]
239 END
240 241 ;-----------------------------------------------------------------
242 ;
243 ;初始化
244 ;
245 FUNCTION ITCffExcel::Init, fileName, Sheet = sheet
246 247 COMPILE_OPT IDL2
248 249 On_Error, 2
250 251 IF (N_Params() LT 1) THEN Message, 'ITCffExcel::Init requires file name'
252 253 ; Use the IDL COM import bridge to init Excel Application Object
254 self.oExcel = Obj_New("IDLcomIDispatch$PROGID$Excel_Application")
255 256 IF ~self.oExcel THEN Return, 0
257 258 ; Get a reference to the Excel Workbook Collection Object
259 self.oExcel->GetProperty, Workbooks = oWkBookCollection
260 self.oWkBookCollection = oWkBookCollection
261 262 self.fileName = fileName
263 264 ; Open the Excel data
265 self.oWkBookData = self.oWkBookCollection->Open(self.fileName)
266 267 ; Get a reference to the sheets collection
268 self.oWkBookData->GetProperty, Sheets = oSheetCollection
269 self.oSheetCollection = oSheetCollection
270 271 ; Parmamerized property - props can
return a value
272 ; (
this syntax used
in collections) - Goofy COM
273 IF (Keyword_Set(sheet)) THEN BEGIN
274 self.oSheetCollection->GetProperty, sheet, Item = oSheet
275 self.oSheet = oSheet
276 ENDIF ELSE BEGIN
277 self.oSheetCollection->GetProperty, 'Sheet1', Item=oSheet
278 self.oSheet = oSheet
279 ENDELSE
280 281 Return, 1
282 END
283 284 ;-----------------------------------------------------------------
285 ;
286 ;定义
287 ;
288 PRO ITCffExcel__Define
289 290 COMPILE_OPT IDL2
291 292 void = {ITCffExcel , $
293 294 ;参数
295 fileName : '' , $
296 297 ;对象
298 oExcel : Obj_New() , $
299 oWkBookCollection : Obj_New() , $
300 oWkBookData : Obj_New() , $
301 oSheetCollection : Obj_New() , $
302 oSheet : Obj_New() , $
303 oCellRange : Obj_New() $
304 }
305 306 END