HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Calling MS Excel vial the Java Native Interface"
Volume: 4 Issue: 11, p. 72

	

Listing 1: AXExcel.java
 
import java.lang.*; 
public class AXExcel extends java.lang.Object { 
static { 
         try { 
                System.loadLibrary("AXExcel"); 
        } 
         catch (UnsatisfiedLinkError e)  { 
                System.out.println(e.getMessage()); 
         } 
         } 
public String getCellValue(String cellName) { 
   String inputValue = 
      JOptionPane.showInputDialog("Cell" +  cellName+ ":"); 
      return inputValue; 
   } 
 public native boolean ExcelGraph(); 
 public native boolean OpenExcelWB(String path,String wks); 
 public native void SetVisibility(boolean flag); 
 public native void QuitExcel(); 
} 
  
  

Listing 2: AXExcel.h
 
#include <jni.h> 
#ifdef --cplusplus 
extern"C"{ 
JNIEXPORT jboolean JNICALL Java_AXExcel_OpenExcelWB 
  (JNIEnv *, jobject, jstring, jstring); 
  

JNIEXPORT void JNICALL Java_AXExcel_SetVisibility 
  (JNIEnv *, jobject, jboolean); 
  

JNIEXPORT void JNICALL Java_AXExcel_QuitExcel 
  (JNIEnv *, jobject); 
  

JNIEXPORT jboolean JNICALL Java_AXExcel_ExcelGraph 
  (JNIEnv *, jobject); 
#ifdef --cplusplus 
} 
  
  

Listing 3: Necessary Includes, Imports and DllMain Function
 
#include "AXExcel.h" 
#include <comdef.h> 
#include <stdio.h> 
#include <objbase.h> 
#include <windows.h> 
#import <mso97.dll>     //mso9.dll for MS Office 2000 
#import <vbeext1.olb>   //vbe6ezt.olb for VB6 
#import <excel8.olb>    //excel9.olb for MS Office 2000 
#pragma warning (disable:4192) 
using namespace Excel; 
_WorksheetPtr pSheet; 
SheetsPtr pSheets; 
_WorkbookPtr pBook; 
_ApplicationPtr pXL; 
BOOL WINAPI DllMain(HINSTANCE hinstDLL, 
DWORD fdwReason, 
       LPVOID lpReserved ) 
{   switch( fdwReason ) 
{   case DLL_PROCESS_ATTACH: 
       CoInitializeEx(NULL,COINIT_MULTITHREADED); 
       break; 
    case DLL_PROCESS_DETACH: 
       CoUninitialize(); 
       break; 
      }    return TRUE; 
} 
  
  

Listing 4: OpenExcelWB
 
JNIEXPORT jboolean JNICALL Java_AXExcel_OpenExcelWB 
  (JNIEnv *jThis, jobject jObj, jstring path, jstring wks) 
{ 
        const char* jbuf = jThis->GetStringUTFChars(path,0); 
        const char* wkname = jThis->GetStringUTFChars(wks,0); 
        jboolean rc = JNI_FALSE; 
        try { 
                if (pXL.GetActiveObject("Excel.Application.8")) { 
                        pXL.CreateInstance("Excel.Application.8"); 
                } 
                WorkbooksPtr pBooks = pXL->Workbooks; 
                try { 
                        pBook = pBooks->Open(jbuf); 
                        pSheets = pBook->GetSheets(); 
                        pSheet = pSheets->GetItem(wkname); 
                        rc = JNI_TRUE; 
                } 
                catch (_com_error &) { 
                        pBook = pBooks->Add(); 
                        pSheet = pBook->Sheets->Add(); 
                        pSheet->Name = wkname; 
                        rc = JNI_TRUE; 
                } 
                pSheet->Activate(); 
        } 
        catch (_com_error &e) { 
                printf("Error: %d, Msg: %s",e.Error(),e.ErrorMessage()); 
        } 
        Java_AXExcel_SetVisibility (jThis, jObj, true); 
        jThis->ReleaseStringUTFChars(path,jbuf); 
        jThis->ReleaseStringUTFChars(wks,wkname); 
        return rc; 
} 
JNIEXPORT void JNICALL Java_AXExcel_SetVisibility 
                (JNIEnv * jThis, jobject jObj,jboolean flag){ 
        try     { 
                if (pXL != NULL){ 
                    if ( flag == JNI_TRUE)  pXL->Visible = VARIANT_TRUE; 
                        else pXL->Visible = VARIANT_FALSE; 
                } 
        } 
        catch(_com_error &e) { 
                printf("Error: %d, Msg: %s",e.Error(),e.ErrorMessage()); 
    } 
} 
  

JNIEXPORT void JNICALL Java_AXExcel_QuitExcel 
  (JNIEnv *, jobject) { 
        try { 
                if (pXL != NULL) { 
                        pBook->Saved = VARIANT_TRUE; 
                        pXL->Quit(); 
                } 
        } 
        catch(_com_error &e) { 
                printf("Error: %d, Msg: %s",e.Error(),e.ErrorMessage()); 
    } 
        if (!pSheet->Release()) pSheet.Detach(); 
        if (!pSheets->Release()) pSheets.Detach(); 
        if (!pBook->Release()) pBook.Detach(); 
        if (!pXL->Release()) pXL.Detach(); 
} 
  
  

Listing 5: ExcelGraph Method
 
JNIEXPORT jboolean JNICALL Java_AXExcel_ExcelGraph 
          (JNIEnv *jThis, jobject jObj){ 
        try     { 
                pSheet->UsedRange->Clear(); 
                RangePtr pRange = pXL->ActiveCell; 
                int textlen; 
                jclass caller = jThis->GetObjectClass(jObj); 
                jmethodID meth = jThis->GetMethodID(caller, 
                        "getCellValue", 
                        "(Ljava/lang/String;)Ljava/lang/String;"); 
                if (meth == 0) { return JNI_FALSE;} 
                do { 
                        _bstr_t cellId = 
                        pRange->GetAddressLocal(false,false,xlA1,false); 
                        jstring range = jThis->NewStringUTF(cellId); 
                        jstring value = 
                          (jstring)jThis->CallObjectMethod(jObj, meth, range); 
                        const char* jbuf = 
                          jThis->GetStringUTFChars(value,0); 
                        textlen = jThis->GetStringUTFLength(value); 
                        pRange->Value = jbuf; 
                        jThis->ReleaseStringUTFChars(value,jbuf); 
                        pRange = pRange->Next; 
                } while (textlen != 0); 
                pRange  = pSheet->GetUsedRange(); 
                _ChartPtr  pChart  = pBook->Charts->Add(); 
                pChart->SetSourceData 
                        (pRange,&variant_t((long)xlColumns)); 
                pChart->Visible; 
                if (!pRange->Release()) pRange.Detach(); 
                if (!pChart->Release()) pChart.Detach(); 
        } 
        catch(_com_error &e) { 
                printf("Error: %d, Msg: %s",e.Error(),e.ErrorMessage()); 
    } 
        return JNI_TRUE; 
} 
  
  
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.