This Question is Possibly Answered

1 "correct" answer available (4 pts) 2 "helpful" answers available (2 pts)
3 Replies Last post: Jun 9, 2009 12:47 PM by Joydeepta  
suresh   2 posts since
Apr 3, 2007
Currently Being Moderated

Apr 3, 2007 8:24 PM

Exception while editing excel

I am trying to run a simple test,  bellow is a source code. While this program is updating the excel sheet which is visible to the user and if user clicks on any cell, I get this following exception. I need to be able to update data to the excel plus user should be able to modify any data.  Is there a way to do this?

 

public class ExcelWithEventTest {

 

      public static void main(String[] args) throws ExcelException

         {

             Application application = new Application();

             application.setVisible(true);

             GenericWorkbook workbook = application.createWorkbook(null);

             Worksheet worksheet = workbook.getWorksheet(1);

             worksheet.setEventHandler(new WorksheetEventHandler() {

 

                    public boolean beforeDoubleClick(WorksheetEventObject event) {

                         int col = event.getCell().getColumn();

                         int row = event.getCell().getRow();

                         System.out.println("Row: " + row + " Col: " + col);

                         return false;

                    }

 

                    public boolean beforeRightClick(WorksheetEventObject event) {

                         int col = event.getCell().getColumn();

                         int row = event.getCell().getRow();

                         System.out.println("Row: " + row + " Col: " + col);

                         return false;

                    }

                  

             });

             Cell cell;

 

             cell = worksheet.getCell("A2");

             String instrument = cell.getString();

             System.out.println("Instrument: " + instrument);

 

             for (int i=0; i< 200000; i++) {

                  worksheet.getCell(1, 2).setValue(i);

                  worksheet.getCell(1, 3).setValue(i+1);

             }

            

            

             try {

                    System.in.read();

               } catch (IOException e) {

                    e.printStackTrace();

               }

             application.close();

         }     

}

 

===============

StackTrade

===============

 

Exception in thread "main" com.jniwrapper.win32.automation.AutomationException: COM object method returns error code: 0x80020009; DISP_E_EXCEPTION (Exception occurred.)

     at com.jniwrapper.win32.com.impl.IUnknownImpl.invokeStandardVirtualMethod(SourceFile:654)

     at com.jniwrapper.win32.automation.impl.IDispatchImpl.invoke(SourceFile:112)

     at com.jniwrapper.win32.automation.Automation$InvocationHelper.a(SourceFile:688)

     at com.jniwrapper.win32.automation.Automation$InvocationHelper.a(SourceFile:672)

     at com.jniwrapper.win32.automation.Automation$InvocationHelper.c(SourceFile:660)

     at com.jniwrapper.win32.automation.Automation$InvocationHelper.setProperty(SourceFile:642)

     at com.jniwrapper.win32.automation.Automation.setProperty(SourceFile:369)

     at com.jniwrapper.win32.automation.Automation.setProperty(SourceFile:395)

     at com.jniwrapper.win32.automation.Automation.setDispatchProperty(SourceFile:447)

     at com.jniwrapper.win32.excel.impl.RangeImpl.setValue2(SourceFile:1979)

     at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)

     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

     at java.lang.reflect.Method.invoke(Unknown Source)

     at com.jniwrapper.win32.automation.m.run(SourceFile:233)

     at com.jniwrapper.win32.MessageLoopThread$ThreadSynchronizedAction.run(MessageLoopThread.java:544)

     at com.jniwrapper.win32.MessageLoopThread$LoopThread.run(MessageLoopThread.java:493)

 

 

 

Thanks,

Suresh

  253 posts since
Apr 24, 2006
Currently Being Moderated
2. Apr 27, 2007 4:30 PM in response to: suresh
Re: Exception while editing excel

Hi Suresh,

 

You try using multithreading calculation in your sample. It seems Excel COM component doesn't support such feature. According to Microsoft announce this feature may be supported in MS Excel 2007:

 

http://blogs.msdn.com/excel/archive/2006/05/26/608453.aspx

 

You can try controlling all user interaction events to provide multithreading emulation but when user inputs data all other operations should be stopped. Similar situation can be observed when Excel executes VBA script: Excel just locks user interaction.

 

Sincerely,

 

Igor Novikov

  1 posts since
Jun 9, 2009
Currently Being Moderated
3. Jun 9, 2009 12:55 PM in response to: Igor Novikov
Re: Exception while editing excel sheet

Hi

 

I am trying to update an excel file with multiple sheets and getting the com exception.

Caused by:

 

 

com.jniwrapper.win32.automation.AutomationException

: COM object method returns error code: 0x80020009; DISP_E_EXCEPTION (Exception occurred.)F52G52H52F55G55H55F56G56H56F57G57H57F62G62H62Closing the application.......

Before writing to second sheet.......

 

In the program , there are two functions each updating a particular sheet .

 

And workbook is closed in the end.

 

 

 

 

 

 



 

 

 

 

 

 

 

 

 

try

 

{

readExcelFile rdexcl =



new readExcelFile();

application =



new Application();

tworkbook = (Workbook) application.openWorkbook(



new File("C://POIForexcel//AnalystSpreadsheet.xls"

));

System.



 

out.print("--------cehck"

+ tworkbook.getWorksheetCount());

tworksheet = tworkbook.getWorksheet(1);

rdexcl.writeExcelForCustomerTotal(CustId, FarmexId , tworksheet);

System.



 

out.println("Closing the application......."

);

 



 

//tworkbook.saveAs(new File("C://POIForexcel//V3Updated_376711.xls"), FileFormat.WORKBOOKNORMAL, true);

 

 

//tworkbook.close(true);

 

 

 

// Projection sheet is having sequence 5

System.

 

out.println("Before writing to second sheet......."

);

 



 

//application = new Application();

 

 

// tworkbook = (Workbook) application.openWorkbook(new File("C://POIForexcel//V3Updated_376711.xls"));

tworksheet1 = tworkbook.getWorksheet(5);

rdexcl.writeExcelForCustomerProjection(CustId, FarmexId , tworksheet1);

 

 

Filename =

 

"C://POIForexcel//"+CustId+"-"+FarmexId+".xls"

;

 



 

// Add Farmex ID to new file

tworkbook.saveAs(

 

new File("C://POIForexcel//V3Updated_376711.xls"), FileFormat.WORKBOOKNORMAL, true

);

 



 

// tworkbook.close(true);





 

 

In creating Projection

 

Could you suggest any alternative?

 

Regards

 

Joydeepta

More Like This

  • Retrieving data ...