1

Problem I'm facing is libraries are not imported like Workbook, Row, Cell, HSSFWork etc. When i try to import its not importing.

Here is link of console: https://prnt.sc/15oajs9

 public class ReusableFunction {
    WebDriver driver;
    public  String [][] fetchDataFromExcel() {
        Workbook wb = null;
        String [][] data= null;
        try {
            String path = fetchprop("KEYWORD_PATH");
            File excel = new File(path);
            FileInputStream file  = new FileInputStream(excel);
            
            System.out.println(path);
            String extn = path.substring(path.indexOf(".")+ 1);
            System.out.println(extn);
            if (extn.equals("xlsx")) {
                wb = new XSSFWorkbook(file);
            } else {
                wb = new HSSFWorkbook(file);
            }
            Sheet sheet = wb.getSheet("Sheet1");
            
            int rownum = sheet.getLastRowNum();
            System.out.println("Rows: " + rownum);
            
            int column = sheet.getRow(0).getLastCellNum();
            
            data  = new String [rownum][column];
            
            for (int i =0; i < rownum; i++) {
                Row row = sheet.getRow(i);
                for (int j =0;  j < column; j++) {
                    Cell cell = row.getCell(j);
                    String value = cell.toString();
                    data[i][j] =  value;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                wb.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        return data;
    }`

Can anyone have idea how we can solve this problem

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Zaman10
  • 81
  • 6
  • Does not look like a maven project, does it ? – cruisepandey Jun 17 '21 at 05:46
  • likely your Apache poi libraries are not detected by eclipse. I would recommend you to go through this stack overflow question to import library to project https://stackoverflow.com/questions/3280353/how-to-import-a-jar-in-eclipse and then may be copy the below import statments and try `import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;` – Anoop Jun 17 '21 at 05:58
  • @Anoop : XSSFWorkbook cannot be resolved to a type Not working, all others are working fine. Here is screenshot: https://prnt.sc/15ohysf – Zaman10 Jun 17 '21 at 06:30
  • @cruisepandey : I have converted it into maven butt problem is same. Here is screenshot: prnt.sc/15ohysf – Zaman10 Jun 17 '21 at 06:31
  • 1
    @cruisepandey : Great! its working. – Zaman10 Jun 17 '21 at 07:14

1 Answers1

1

add these dependency in your pom.xml file :

poi

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.0.0</version>
</dependency>

poi-oxml :

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.0.0</version>
</dependency>

commons-collections :

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.4</version>
</dependency>

commons-compress :

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.20</version>
</dependency>

xmlbeans

<!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
<dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>5.0.0</version>
</dependency>

dom4j

<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>1.6.1</version>
</dependency>

Build your project and then run, should work for you.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38