-3

I'm really confused I've read many articles but none helped, I want to know how to read data from Excel using java (I'm using NetBeans). I need to know which library to use, and source code. I appreciate your help.

mona
  • 53
  • 1
  • 1
  • 7
  • Possible duplicate of [How to read and write excel file](https://stackoverflow.com/questions/1516144/how-to-read-and-write-excel-file) – Arnaud Jul 10 '18 at 07:27
  • Thanks Berger, but I downloaded apache POI but I have an error (package org.wickedsource.docxstamper.DocxStamper doesn't exist). I already downloaded the library. – mona Jul 10 '18 at 07:33

1 Answers1

2

You can use Apache Poi to read Excel, Word, etc.

There is a good tutorial on TutorialsPoint Apache Poi Quick Guide, you should check it out there first.

Below, there is an example code for you, it reads from the WriteSheet.xlsx excel file and prints all the rows-columns to console:

FileInputStream fis = new FileInputStream(new File("WriteSheet.xlsx"));

  XSSFWorkbook workbook = new XSSFWorkbook(fis);
  XSSFSheet spreadsheet = workbook.getSheetAt(0);
  Iterator < Row >  rowIterator = spreadsheet.iterator();

  while (rowIterator.hasNext()) {
     row = (XSSFRow) rowIterator.next();
     Iterator < Cell >  cellIterator = row.cellIterator();

     while ( cellIterator.hasNext()) {
        Cell cell = cellIterator.next();

        switch (cell.getCellType()) {
           case Cell.CELL_TYPE_NUMERIC:
              System.out.print(cell.getNumericCellValue() + " \t\t ");
              break;

           case Cell.CELL_TYPE_STRING:
              System.out.print(
              cell.getStringCellValue() + " \t\t ");
              break;
        }
     }
     System.out.println();
  }
  fis.close();

Edit: You should check out this link if you have got problems in adding jar file to netbeans.

But, if you have got problems in adding the necessary jar files, first you should download the poi-bin zip file in the Apache Poi Download Page. After that, you should add these jars to your env(Versions may vary in time):

poi-3.17.jar
ooxml-lib/xmlbeans-2.6.0.jar
poi-ooxml-3.17.jar
poi-ooxml-schemas-3.17.jar
lib/commons-collections4-4.1.jar
  • 1
    Hi Burak, could you please explain how can I downlaod Apache Poi on mac (netbeans). I'm confused in that. thanks! – mona Jul 10 '18 at 07:43
  • 1
    Hi Mona, I've updated my answer because of I couldn't fit my reply in the comments section. Hope it helps :) – Burak Demirbilek Jul 10 '18 at 08:02