9 #import "c:/program files (x86)/common files/microsoft shared/office12/mso.dll" \
\r
10 rename( "RGB", "MSORGB" )
\r
12 using namespace Office;
\r
14 #import "c:/program files (x86)/common files/microsoft shared/vba/vba6/vbe6ext.olb" \
\r
16 using namespace VBIDE;
\r
18 #import "c:/program files (x86)/microsoft office/office12/excel.exe" \
\r
19 rename( "DialogBox", "ExcelDialogBox" ) \
\r
20 rename( "RGB", "ExcelRGB" ) \
\r
21 rename( "CopyFile", "ExcelCopyFile" ) \
\r
22 rename( "ReplaceText", "ExcelReplaceText" )
\r
26 //#define SIMANTICS_DEBUG(...) \
\r
27 //{ FILE *fp = fopen("d:/excel.log", "ab"); fprintf(fp, __VA_ARGS__); fflush(fp); fclose(fp); }
\r
29 #define SIMANTICS_DEBUG(...)
\r
31 using namespace std;
\r
32 using namespace Excel;
\r
34 map<int, Excel::_WorksheetPtr> handles;
\r
35 static int handleCounter = 1;
\r
37 void replace(std::string &s, const std::string &s1, const std::string &s2) {
\r
41 index = s.find(s1, index);
\r
42 while(index != std::string::npos) {
\r
43 s.replace(index, s1.length(), s2.c_str(), s2.length());
\r
44 index = s.find(s1, index + 1);
\r
49 boolean getString(JNIEnv *env, jstring str, char *target) {
\r
52 const char *chars = env->GetStringUTFChars(str, &iscopy);
\r
53 if(!chars) return false;
\r
55 strcpy(target, chars);
\r
57 env->ReleaseStringUTFChars(str, chars);
\r
63 string getString(JNIEnv *env, jstring str) {
\r
66 const char *chars = env->GetStringUTFChars(str, &iscopy);
\r
67 if(!chars) return 0;
\r
68 string result = chars;
\r
69 env->ReleaseStringUTFChars(str, chars);
\r
74 Excel::_ApplicationPtr getApplication(bool create) {
\r
76 Excel::_ApplicationPtr application;
\r
79 HRESULT hr = CLSIDFromProgID(L"Excel.Application", &clsid);
\r
84 if (!SUCCEEDED(application.GetActiveObject(clsid)))
\r
87 hr = application.CreateInstance(clsid);
\r
96 application->EnableEvents = VARIANT_FALSE;
\r
102 JNIEXPORT jint JNICALL Java_org_simantics_excel_Excel_init(JNIEnv *env, jobject) {
\r
104 SIMANTICS_DEBUG("Excel init\r\n");
\r
106 HRESULT hr = ::OleInitialize(NULL);
\r
112 Excel::_ApplicationPtr application = getApplication(true);
\r
113 if(!application) return -1;
\r
114 bstr_t version = application->GetVersion(0);
\r
116 os << version << endl;
\r
117 SIMANTICS_DEBUG("Init successful with version %s\r\n", os.str().c_str());
\r
119 application->put_Interactive( 0, VARIANT_FALSE );
\r
120 application->put_Visible( 0, VARIANT_FALSE );
\r
127 Excel::_WorkbookPtr getWorkbook(const Excel::_ApplicationPtr &application, const string &fileName) {
\r
129 SIMANTICS_DEBUG("getWorkbook(%s)\r\n", fileName.c_str());
\r
131 for(int i=0;i<application->Workbooks->Count;i++) {
\r
133 Excel::_WorkbookPtr workbook = application->Workbooks->GetItem( _variant_t(i+1) );
\r
134 workbook->get_FullName(0, &fullName.GetBSTR());
\r
135 std::string fullNameStr = fullName;
\r
136 SIMANTICS_DEBUG("'%s'\r\n", fullNameStr.c_str());
\r
137 if(fullNameStr == fileName) return workbook;
\r
144 Excel::_WorksheetPtr getWorksheet(const Excel::_WorkbookPtr workbook, const string &sheetName) {
\r
146 SIMANTICS_DEBUG("getWorksheet(%s)\r\n", sheetName.c_str());
\r
148 for(int i=0;i<workbook->Sheets->Count;i++) {
\r
149 Excel::_WorksheetPtr sheet = workbook->Sheets->GetItem(_variant_t(i+1));
\r
150 std:string name = sheet->Name;
\r
151 SIMANTICS_DEBUG("'%s'\r\n", name.c_str());
\r
152 if(name == sheetName) return sheet;
\r
159 char message[1024];
\r
161 JNIEXPORT jstring JNICALL Java_org_simantics_excel_Excel_open(JNIEnv *env, jobject, jstring fileName, jstring sheetName_) {
\r
163 string name = getString(env, fileName);
\r
164 string sheetName = getString(env, sheetName_);
\r
166 _variant_t varOption( (long) DISP_E_PARAMNOTFOUND, VT_ERROR );
\r
168 Excel::_ApplicationPtr application = getApplication(true);
\r
169 if(!application) return env->NewStringUTF("Excel application could not be started.");
\r
171 Excel::_WorkbookPtr workbook = getWorkbook(application, name);
\r
174 SIMANTICS_DEBUG("getWorkbook(%s) book was not found. Opening.\r\n", name.c_str());
\r
176 FILE *fp = fopen(name.c_str(), "r");
\r
179 SIMANTICS_DEBUG("getWorkbook(%s) book was found. Opening file.\r\n", name.c_str());
\r
182 workbook = application->Workbooks->OpenXML(_bstr_t(name.c_str()),
\r
183 varOption, varOption);
\r
184 if(!workbook) return env->NewStringUTF("File could not be opened.");
\r
188 SIMANTICS_DEBUG("getWorkbook(%s) book was not found. Creating file.\r\n", name.c_str());
\r
190 workbook = application->Workbooks->Add(varOption);
\r
192 if(!workbook) return env->NewStringUTF("New workbook could not be created.");
\r
196 Excel::_WorksheetPtr sheet = workbook->Sheets->Add();
\r
197 sheet->Name = _bstr_t(sheetName.c_str());
\r
200 // Does not work for some unknown reason
\r
201 for(int i=0;i<workbook->Sheets->Count;i++) {
\r
202 Excel::_WorksheetPtr sheet = workbook->Sheets->GetItem(_variant_t(i+1));
\r
203 if(name != sheetName)
\r
204 sheet->PutVisible(0, Excel::XlSheetVisibility::xlSheetVeryHidden);
\r
208 workbook->SaveAs(_bstr_t(name.c_str()), varOption, varOption, varOption, varOption,
\r
209 varOption, Excel::xlExclusive, varOption, varOption, varOption, varOption, 0);
\r
211 } catch (_com_error& e) {
\r
212 _bstr_t bstr = _bstr_t(e.ErrorMessage());
\r
214 _bstr_t bstr2 = _bstr_t(e.Description());
\r
216 SIMANTICS_DEBUG("getWorkbook(%s) book was not found. Creation error 1 '%s' '%s'\r\n", name.c_str(), s.c_str(), s2.c_str());
\r
217 strcpy(message, s2.c_str());
\r
218 return env->NewStringUTF(message);
\r
219 } catch (exception& e) {
\r
220 SIMANTICS_DEBUG("getWorkbook(%s) book was not found. Creation error 2 '%s'\r\n", name.c_str(), e.what());
\r
221 return env->NewStringUTF(e.what());
\r
223 SIMANTICS_DEBUG("getWorkbook(%s) book was not found. Creation error 3 \r\n", name.c_str());
\r
224 return env->NewStringUTF("Undefined error");
\r
232 application->put_Interactive( 0, VARIANT_TRUE );
\r
233 application->put_Visible( 0, VARIANT_TRUE );
\r
235 workbook->Activate();
\r
237 SIMANTICS_DEBUG("getWorkbook(%s) searches for sheet '%s'\r\n", name.c_str(), sheetName.c_str());
\r
240 Excel::_WorksheetPtr sheet = getWorksheet(workbook, sheetName);
\r
242 SIMANTICS_DEBUG("creating sheet %s for '%s'\r\n", sheetName.c_str(), name.c_str());
\r
243 sheet = workbook->Sheets->Add();
\r
244 sheet->Name = _bstr_t(sheetName.c_str());
\r
246 int handle = handleCounter++;
\r
247 SIMANTICS_DEBUG("opened handle %d for '%s'\r\n", handle, name.c_str());
\r
248 handles[handle] = sheet;
\r
251 return env->NewStringUTF(os.str().c_str());
\r
253 } catch (_com_error& e) {
\r
254 _bstr_t bstr = _bstr_t(e.ErrorMessage());
\r
256 _bstr_t bstr2 = _bstr_t(e.Description());
\r
258 SIMANTICS_DEBUG("getWorkbook(%s) book was not found. Creation error 1 '%s' '%s'\r\n", name.c_str(), s.c_str(), s2.c_str());
\r
259 return env->NewStringUTF(s2.c_str());
\r
260 } catch (exception& e) {
\r
261 SIMANTICS_DEBUG("getWorkbook(%s) book was not found. Creation error 2 '%s'\r\n", name.c_str(), e.what());
\r
262 return env->NewStringUTF(e.what());
\r
264 SIMANTICS_DEBUG("getWorkbook(%s) book was not found. Creation error\r\n", name.c_str());
\r
265 return env->NewStringUTF("Unhandled exception.");
\r
269 //Excel::_WorksheetPtr sheet = workbook->ActiveSheet;
\r
270 //if(!sheet) return -4;
\r
275 string cellName(int row, int column) {
\r
279 SIMANTICS_DEBUG("cellName(%d, %d) -> ", row, column);
\r
282 char rem = column % 26;
\r
283 char c = 'A' + rem;
\r
291 result += os.str();
\r
293 SIMANTICS_DEBUG("%s\r\n", result.c_str());
\r
300 Excel::_WorkbookPtr getWorkbook(int handle) {
\r
301 map<int, Excel::_WorkbookPtr>::iterator it = handles.find(handle);
\r
302 if(it != handles.end()) return (*it).second;
\r
307 Excel::_WorksheetPtr getWorksheet(int handle) {
\r
308 map<int, Excel::_WorksheetPtr>::iterator it = handles.find(handle);
\r
309 if(it != handles.end()) return (*it).second;
\r
313 JNIEXPORT jint JNICALL Java_org_simantics_excel_Excel_setDouble(JNIEnv *env, jobject, jint handle, jint row, jint column, jdouble value) {
\r
315 SIMANTICS_DEBUG("setDouble(%d, %d, %f)\r\n", row, column, value);
\r
317 Excel::_WorksheetPtr sheet = getWorksheet(handle);
\r
318 if(!sheet) return -1;
\r
319 Excel::RangePtr range = sheet->Cells;
\r
320 if(!range) return -2;
\r
321 range->Item[row+1][column+1] = value;
\r
327 JNIEXPORT jint JNICALL Java_org_simantics_excel_Excel_setString(JNIEnv *env, jobject, jint handle, jint row, jint column, jstring value) {
\r
331 string val = getString(env, value);
\r
333 SIMANTICS_DEBUG("setString(%d, %d, %s)\r\n", row, column, val.c_str());
\r
335 Excel::_WorksheetPtr sheet = getWorksheet(handle);
\r
337 SIMANTICS_DEBUG("setString2(%d, %d, %s)\r\n", row, column, val.c_str());
\r
339 if(!sheet) return -1;
\r
341 struct Range *_range = 0;
\r
342 HRESULT _hr = sheet->get_Cells(&_range);
\r
343 if (FAILED(_hr)) return -1;
\r
344 Excel::RangePtr range = RangePtr(_range, false);
\r
346 SIMANTICS_DEBUG("setString3(%d, %d, %s)\r\n", row, column, val.c_str());
\r
348 if(!range) return -2;
\r
350 const char *text = val.c_str();
\r
352 SIMANTICS_DEBUG("setString4(%d, %d, %s)\r\n", row, column, val.c_str());
\r
354 Excel::RangePtr cell = range->Item[row+1][column+1];
\r
355 if(!cell) return -4;
\r
357 SIMANTICS_DEBUG("setString5(%d, %d, %s)\r\n", row, column, val.c_str());
\r
359 cell->Item[1][1] = text;
\r
365 } catch (_com_error& e) {
\r
367 _bstr_t bstr = _bstr_t(e.ErrorMessage());
\r
369 _bstr_t bstr2 = _bstr_t(e.Description());
\r
371 SIMANTICS_DEBUG("SetString error 1 '%s' '%s'\r\n", s.c_str(), s2.c_str());
\r
374 } catch (exception& e) {
\r
376 SIMANTICS_DEBUG("SetString error 2 '%s'\r\n", e.what());
\r
381 SIMANTICS_DEBUG("SetString error\r\n");
\r
388 JNIEXPORT jint JNICALL Java_org_simantics_excel_Excel_setName(JNIEnv *env, jobject, jint handle, jint row, jint column, jstring value) {
\r
390 string val = getString(env, value);
\r
392 SIMANTICS_DEBUG("setName(%d, %d, %s)\r\n", row, column, val.c_str());
\r
394 Excel::_WorksheetPtr sheet = getWorksheet(handle);
\r
395 if(!sheet) return -1;
\r
397 Excel::RangePtr range = sheet->GetRange(_variant_t(cellName(row, column).c_str()));
\r
398 if(!range) return -2;
\r
399 range->Name = _variant_t(val.c_str());
\r
402 //range->Item[row+1][column+1] = text;
\r
403 //range->Name[row+1][column+1] = text;
\r
409 JNIEXPORT jint JNICALL Java_org_simantics_excel_Excel_setVisible(JNIEnv *env, jobject, jint handle, jboolean value) {
\r
411 SIMANTICS_DEBUG("setVisible(%d)\r\n", value);
\r
413 Excel::_WorksheetPtr sheet = getWorksheet(handle);
\r
414 if(!sheet) return -1;
\r
415 Excel::_ApplicationPtr application = sheet->Application;
\r
416 if(!application) return -2;
\r
419 application->put_Interactive( 0, VARIANT_TRUE );
\r
420 application->put_Visible( 0, VARIANT_TRUE );
\r
422 application->put_Interactive( 0, VARIANT_FALSE );
\r
423 application->put_Visible( 0, VARIANT_FALSE );
\r
430 JNIEXPORT jint JNICALL Java_org_simantics_excel_Excel_close(JNIEnv *env, jobject, jint handle) {
\r
432 SIMANTICS_DEBUG("closing handle %d\r\n", handle);
\r
436 Excel::_WorksheetPtr sheet = getWorksheet(handle);
\r
437 if(!sheet) return -1;
\r
439 // Release this reference for Close
\r
440 handles.erase(handle);
\r
442 SIMANTICS_DEBUG("closing 1\r\n");
\r
444 struct Range *_range = 0;
\r
445 HRESULT _hr = sheet->get_Cells(&_range);
\r
446 if (FAILED(_hr)) return -1;
\r
448 SIMANTICS_DEBUG("closing 2\r\n");
\r
450 Excel::_ApplicationPtr application = sheet->Application;
\r
451 if(!application) return -2;
\r
453 Excel::_WorkbookPtr workbook = (Excel::_WorkbookPtr)sheet->Parent;
\r
454 if(!workbook) return -3;
\r
456 // SIMANTICS_DEBUG("About to save\r\n");
\r
457 HRESULT hr = workbook->Save();
\r
458 // workbook->put_Saved(0, VARIANT_TRUE);
\r
460 SIMANTICS_DEBUG("Save failed with %d\r\n", hr);
\r
464 map<int, Excel::_WorksheetPtr>::iterator it = handles.begin();
\r
465 for(;it != handles.end();++it) {
\r
466 Excel::_WorksheetPtr sheet2 = (*it).second;
\r
467 Excel::_WorkbookPtr book = (Excel::_WorkbookPtr)sheet2->Parent;
\r
468 // The book remains open
\r
469 if(book->Name == workbook->Name) return 0;
\r
472 SIMANTICS_DEBUG("About to Close\r\n");
\r
473 hr = workbook->Close(VARIANT_FALSE);
\r
475 SIMANTICS_DEBUG("Close failed with %d\r\n", hr);
\r
479 long remaining = 0;
\r
481 application->Workbooks->get_Count(&remaining);
\r
483 application->put_Interactive( 0, VARIANT_FALSE );
\r
484 application->put_Visible( 0, VARIANT_FALSE );
\r
485 hr = application->Quit();
\r
487 SIMANTICS_DEBUG("Close failed with %d\r\n", hr);
\r
492 } catch (_com_error& e) {
\r
493 _bstr_t bstr = _bstr_t(e.ErrorMessage());
\r
495 _bstr_t bstr2 = _bstr_t(e.Description());
\r
497 SIMANTICS_DEBUG("Close error 1 '%s' '%s'\r\n", s.c_str(), s2.c_str());
\r
499 } catch (exception& e) {
\r
500 SIMANTICS_DEBUG("Close error 2 '%s'\r\n", e.what());
\r
503 SIMANTICS_DEBUG("Close error\r\n");
\r
509 // SIMANTICS_DEBUG("About to hide");
\r
511 // application->put_Interactive( 0, VARIANT_FALSE );
\r
512 // application->put_Visible( 0, VARIANT_FALSE );
\r
514 // if(handles.empty())
\r
515 // application->Quit();
\r