2 Replies Last post: Jun 3, 2009 11:02 AM by Santanu Saha  
  2 posts since
Jun 1, 2009
Currently Being Moderated

Jun 1, 2009 3:54 PM

Problem with WorkEventHandler

I am using JExcel for opening an existing excel. I need to trigger excel SAVE and CLOSE operation. I am using the below code for that. Although the excel is opening fine but the beforeClose()

and beforeSave() methods are not getting called. The sample code that I am using is given below. Can anyone help whats wrong about my code.

 

 

 

 





public

 

class

Example1 {

 



 

public static void main(String[] args) throws

ExcelException, IOException

{

Application appln =



 

new

Application();

appln.setVisible(



 

true

);

 

Workbook wb = appln.openWorkbook(



 

new File("C:/analystexcels/abcd.xls"

));

 

wb.setEventHandler(



 

new

WorkbookEventHandler()

{

 

 



 

public boolean

beforeClose(WorkbookEventObject arg0) {

 



 

// TODO

Auto-generated method stub

System.

 

out.println("@@@@@@ Inside before CLOSE..."

);

 



 

return false

;

}

 

 



 

public boolean

beforeSave(WorkbookEventObject arg0) {

 



 

// TODO

Auto-generated method stub

System.

 

out.println("@@@@@@ Inside before SAVE"

);

 



 

return false

;





Serge Piletsky TeamDev Ltd. 1,066 posts since
Apr 24, 2006
Currently Being Moderated
1. Jun 2, 2009 3:32 PM in response to: Santanu Saha
Re: Problem with WorkEventHandler

sscsju wrote:

 

Can anyone help whats wrong about my code.

First of all, it's formatted too much. There is the "Insert" toolbar button which allows to instert any formatted text, indluding highligned Java code. Just click on "Syntax Highligting -> Java" menu item.

 

Secondly, this code seems to be inclomplete; apparently applicaiton just will exit after registering an event handler.

 

Here is the modified code that should be working correctly:

public static void main(String[] args) throws ExcelException, IOException
{
    Application application = new Application();
    application.setVisible(true);
    Workbook workbook = application.openWorkbook(new File("c:\\Book1.xls "));
    workbook.setEventHandler(new WorkbookEventHandler() {
         public boolean beforeClose(WorkbookEventObject arg0) {
            System.out.println("@@@@@@ Inside before CLOSE...");
            return false;
        }
 
        public boolean beforeSave(WorkbookEventObject arg0) {
            System.out.println("@@@@@@ Inside before SAVE");
            return false;
        }
    });
 
    System.out.println("Press 'Enter' to terminate application");
    System.in.read();
    application.setVisible(false);
    application.close();
    System.out.println("Done.");
}

 

Hope this helps.

 

-Serge

More Like This

  • Retrieving data ...