Here is how I extract a HSSFWorkbook object from a excel file (note: I use
log4j oppose to System.out):
public HSSFWorkbook loadFile(String file) {
HSSFWorkbook iworkbook = null;
try {
InputStream idocument = new FileInputStream(file);
log.debug("File exists, reading...");
POIFSFileSystem idata = new
POIFSFileSystem(idocument);
log.debug("File read.");
iworkbook = new HSSFWorkbook(idata);
log.debug("Excel Workbook found.");
idocument.close();
} catch (FileNotFoundException e) {
//e.printStackTrace();
log.fatal("File " + file + " can not be found.");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
log.fatal("File or workbook " + file + " can not be
read.");
System.exit(0);
}
return (iworkbook);
}
I'm not sure why you catch IOException and create a new IOException to be
thrown. What are you attempting to do with that if you don't mind me
asking?
Jonathan Paul Cowherd
Linux and Java Administrator
Genscape, Inc.
Email: [EMAIL PROTECTED]
Office: (502) 583-3730
Mobile: (502) 314-0444
> -----Original Message-----
> From: Ramirez, Oscar [mailto:[EMAIL PROTECTED]
> Sent: Friday, April 04, 2003 9:57 AM
> To: 'Vikranth'
> Cc: 'POI User Group'
> Subject: RE: Password-protected Excel Workbook
>
>
> Vikranth,
>
> I tried and got a very nasty exception. My code reads as follows:
>
> HSSFSheet sheet;
>
> public POIWizardExcelParser(String fileName) throws IOException
> {
> FileInputStream fin;
> POIFSFileSystem fs;
> HSSFWorkbook wb;
>
> try
> {
> fin = new FileInputStream(fileName);
> }
> catch (IOException io)
> {
> throw new IOException("Cannot open file: " +
> fileName + "\n" + io.getMessage());
> }
>
> try
> {
> fs = new POIFSFileSystem(fin);
> }
> catch (Exception e)
> {
> throw new IOException("Cannot create a
> POIFS File
> System.\n" + e.getMessage());
> }
>
> try
> {
> wb = new HSSFWorkbook(fs);
> }
> catch (Exception e)
> {
> throw new IOException("Cannot create a
> Workbook.\n"
> + e.getMessage());
> }
>
> sheet = wb.getSheetAt(0);
> }
>
> I get the following error when creating a new HSSFWorkbook:
>
>
> java.lang.reflect.InvocationTargetException
> at
> sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
> Method)
> at
> sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeCo
> nstructorAcces
> sorImpl.java:39)
> at
> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Dele
> gatingConstruc
> torAccessorImpl.java:27)
> at
> java.lang.reflect.Constructor.newInstance(Constructor.java:274)
> at
> org.apache.poi.hssf.record.RecordFactory.createRecord(RecordFa
> ctory.java:246
> )
> at
> org.apache.poi.hssf.record.RecordFactory.createRecords(RecordF
> actory.java:18
> 4)
> at
> org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook
> .java:140)
> at
> com.gs.eq.mot.gui.base.POIWizardExcelParser.<init>(POIWizardEx
> celParser.java
> :49)
> at
> com.gs.eq.mot.gui.wizard.views.InputPanel.setWizardData(InputP
> anel.java:109)
> at
> com.gs.eq.mot.gui.wizard.controller.WizardController.forwardCo
> mmand(WizardCo
> ntroller.java:221)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccess
> orImpl.java:39
> )
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMeth
> odAccessorImpl
> .java:25)
> at java.lang.reflect.Method.invoke(Method.java:324)
> at
> com.gs.eq.mot.gui.base.GenericListener$1.invoke(GenericListene
> r.java:56)
> at $Proxy0.actionPerformed(Unknown Source)
> at
> com.gs.eq.mot.gui.base.JLFAbstractAction.actionPerformed(JLFAb
> stractAction.j
> ava:99)
> at
> javax.swing.AbstractButton.fireActionPerformed(AbstractButton.
> java:1764)
> at
> javax.swing.AbstractButton$ForwardActionEvents.actionPerformed
> (AbstractButto
> n.java:1817)
> at
> javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButt
> onModel.java:4
> 19)
> at
> javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257)
> at
> javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Basic
> ButtonListener
> .java:245)
> at
> java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster
> .java:227)
> at
> java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster
> .java:227)
> at java.awt.Component.processMouseEvent(Component.java:5093)
> at java.awt.Component.processEvent(Component.java:4890)
> at java.awt.Container.processEvent(Container.java:1566)
> at java.awt.Component.dispatchEventImpl(Component.java:3598)
> at java.awt.Container.dispatchEventImpl(Container.java:1623)
> at java.awt.Component.dispatchEvent(Component.java:3439)
> at
> java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3450)
> at
> java.awt.LightweightDispatcher.processMouseEvent(Container.java:3165)
> at
> java.awt.LightweightDispatcher.dispatchEvent(Container.java:3095)
> at java.awt.Container.dispatchEventImpl(Container.java:1609)
> at java.awt.Window.dispatchEventImpl(Window.java:1585)
> at java.awt.Component.dispatchEvent(Component.java:3439)
> at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
> at
> java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDis
> patchThread.ja
> va:197)
> at
> java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispa
> tchThread.java
> :150)
> at
> java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
> at
> java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
> at
> java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
> Caused by: java.lang.IllegalArgumentException: Illegal length
> at
> org.apache.poi.util.StringUtil.getFromUnicode(StringUtil.java:107)
> at
> org.apache.poi.hssf.record.FontRecord.fillFields(FontRecord.java:172)
> at
> org.apache.poi.hssf.record.Record.fillFields(Record.java:142)
> at org.apache.poi.hssf.record.Record.<init>(Record.java:104)
> at
> org.apache.poi.hssf.record.FontRecord.<init>(FontRecord.java:125)
>
> I am curious how you got around it.
>
> Oscar A. Ramirez
> Goldman, Sachs & Co.
> Voice: (212) 357-7402
> Email: [EMAIL PROTECTED]
>
>
> -----Original Message-----
> From: Vikranth [mailto:[EMAIL PROTECTED]
> Sent: Friday, April 04, 2003 12:49 AM
> To: Ramirez, Oscar
> Subject: Password-protected Excel Workbook
>
>
> Hello Oscar,
> You just forget that the excel file is password protected.
> You will still
> be able to read and write to the excel file even if it is password
> protected. I have tried it out ..
> Regards,
> Vikranth
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>