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
;
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
Thanks Serge. This definitely solves the problem.
Actually I am using JExcel to open an existing spreadsheet from a JFrame on click of JButton. I need to capture the SAVE and CLOSE event of excel. I need to copy a new version of the spreadsheet once user makes some changes in the excel and clicks the Save button. The problem I am facing is every time I click the button one excel spreadsheet is opening and one EXCEL.EXE process is starting. But on close of each excel spreadsheet the particular EXCEL.EXE process does not exit. It remains till I close the JFrame. Is it possible to close each EXCEL.EXE process once I close the spreadsheet. If so can you please help on this.