0

I have written a very simple code to read from an Excel file, that is currently on my local C drive. I am getting the java.lang.reflect.InvocationTargetException, and after several methods suggested on stackoverflow i still can't get it to read from the file.

Here is the code:

package com.rrd.rism.test.sel.specdetailspage;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.Test;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;

    import java.awt.AWTException;
    import java.awt.Robot;
    import java.awt.event.KeyEvent;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.sql.Array;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Properties;
    import java.util.concurrent.TimeUnit;

    import org.apache.commons.io.output.ByteArrayOutputStream;
    import org.apache.poi.hssf.util.HSSFColor;
    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
    import org.apache.poi.ss.formula.functions.Column;
    import org.apache.poi.ss.usermodel.CellStyle;
    import org.apache.poi.ss.usermodel.Font;
    import org.apache.poi.ss.usermodel.IndexedColors;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.usermodel.WorkbookFactory;
    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFCellStyle;
    import org.apache.poi.xssf.usermodel.XSSFColor;
    import org.apache.poi.xssf.usermodel.XSSFFont;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.interactions.Actions;
    import org.testng.annotations.Test;



public class roleValidationforstatus 
{


    @Test
    public void f() throws IOException 
    {


      FileInputStream fis=new FileInputStream("C:\\**path**\\filename.xlsx");
      XSSFWorkbook wb = new XSSFWorkbook(fis);
      XSSFSheet sheet = wb.getSheetAt(0);

      int rowCount = sheet.getPhysicalNumberOfRows();
      System.out.println(rowCount);

      for (int a =1; a<=rowCount; a++)
      {
          String userName = "";
          XSSFRow row1 = sheet.getRow(a);
          XSSFCell cell1 = row1.getCell(1);
          userName = cell1.getStringCellValue();
          System.out.println(userName);
      }       
   }

}

Here is the list of included .jar files that i currently use:

List of Jar files

This is the error i am getting:

    FAILED: f
    org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
        at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:62)
        at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:427)
        at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:162)
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:263)
        at com.rrd.rism.test.sel.specdetailspage.roleValidationforstatus.f(roleValidationforstatus.java:75)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
        at org.testng.TestRunner.privateRun(TestRunner.java:782)
        at org.testng.TestRunner.run(TestRunner.java:632)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
        at org.testng.SuiteRunner.run(SuiteRunner.java:268)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
        at org.testng.TestNG.run(TestNG.java:1064)
        at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)
    Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60)
        ... 28 more
    Caused by: java.lang.NoClassDefFoundError: org/openxmlformats/schemas/spreadsheetml/x2006/main/StyleSheetDocument$Factory
        at org.apache.poi.xssf.model.StylesTable.readFrom(StylesTable.java:125)
        at org.apache.poi.xssf.model.StylesTable.<init>(StylesTable.java:96)
        ... 33 more
    Caused by: java.lang.ClassNotFoundException: org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument$Factory
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 35 more

I have already tired the following things:

-Installed newer version of XML beans

-File is present and accessible and the system can find it

-Made sure that all the depend .jar file are from the same version (11 poi, ooxml etc.)

I would also like to add that i dont have excel installed on this machine, as my company doesn't have license for it. So i have created this file using google drive and read the file using Excel viewer.

Please assist. Thank you.

jzee
  • 1
  • 2

1 Answers1

0

You need the following four jars:

  • poi-3.13-20150929.jar
  • poi-ooxml-3.13-20150929.jar
  • poi-ooxml-schemas-3.13-20150929.jar
  • ooxml-lib\xmlbeans-2.6.0.jar

ooxml-lib\dom4j-1.6.1.jar (is no longer required in latest poi versions)

Quick start link

Updated to latest jar dates and versions. Thank you @Gagravarr

parishodak
  • 4,506
  • 4
  • 34
  • 48