Hi Iam trying to read excel file as per the program (downloaded from internet, attached for reference here) But i am getting below error: got 'SummaryInformation' event for path '\'. got 'DocumentSummaryInformation' event for path '\'. got 'Workbook' event for path '\'. trying DirectoryNode 'Root Entry' Exception in thread "main" java.lang.NoSuchMethodError: org.apache.poi.POIDocument.<init>(Lorg/apache/poi/poifs/filesystem/DirectoryNode;Lorg/apache/poi/poifs/filesystem/POIFSFileSystem;)V at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:225) at com.lehman.pcs.bcu.util.POIFSExtract$MyPOIFSReaderListener.processPOIFSReaderEvent(POIFSExtract.java:53) at org.apache.poi.poifs.eventfilesystem.POIFSReader.processProperties(POIFSReader.java:261) at org.apache.poi.poifs.eventfilesystem.POIFSReader.read(POIFSReader.java:97) at com.lehman.pcs.bcu.util.POIFSExtract.main(POIFSExtract.java:29) I am using jars of version as below : poi-3.1-FINAL.jar poi-scratchpad-3.0.1-FINAL.jar poi-contrib-3.1-FINAL.jar
The attached code is pasted below. package com.lehman.pcs.bcu.util; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import org.apache.poi.hssf.record.RecordFormatException; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.eventfilesystem.POIFSReader; import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent; import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.POIFSDocumentPath; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class POIFSExtract { public static void main(String[] args) throws Exception { //if (args == null || args.length < 1) //throw new Exception("\n->no input, no output<-"); String fileName1 = "H:\\Kishor\\Research\\EmailWorkFlow\\Web_Server_Comparison.xls"; POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName1)); POIFSReader reader = new POIFSReader(); reader.registerListener(new MyPOIFSReaderListener(fs)); reader.read(new FileInputStream(fileName1)); } static class MyPOIFSReaderListener implements POIFSReaderListener { private POIFSFileSystem fs; public MyPOIFSReaderListener(POIFSFileSystem fs) { this.fs = fs; } public void processPOIFSReaderEvent(POIFSReaderEvent event) { String name = event.getName(); POIFSDocumentPath path = event.getPath(); System.out.println("got '" + name + "' event for path '" + path + "'."); if (name.endsWith("Workbook") || name.endsWith("WORKBOOK")) { try { DirectoryNode dir = resolveDir(fs, event.getPath().toString()); // converting it to DirectoryNode System.out.println(" trying DirectoryNode '" + dir.getName() + "'"); HSSFWorkbook wb = new HSSFWorkbook(dir, fs, true); //invoke HSSFWorkbook constructor passing this directory to it System.out.println(" !!! success: workbook with " + wb.getNumberOfSheets() + " sheets."); } catch (RecordFormatException e) { // MS Graph charts are stored in "Workbook" stream too System.out.println(" skipping embedded MS Graph object!"); } catch (Exception e) { System.out.println(" " + e.getMessage()); throw new RuntimeException(e.getMessage()); } } } static DirectoryNode resolveDir(POIFSFileSystem filesystem, String path) throws FileNotFoundException { DirectoryNode dir = filesystem.getRoot(); for (String token : path.split("\\" + File.separator)) { if (!token.equals("") && !token.equals(File.separator)) dir = (DirectoryNode) dir.getEntry(token); } return dir; } } }
You almost certainly have a different version of poi on your run classpath to your compile classpath. Please see the faq on the site for how to test which jars you're really running with.