reading excel file --> xlsx format with java

◇◆丶佛笑我妖孽 提交于 2019-12-06 06:57:45
Benoit Wickramarachi

Apache POI is a good librairy to read xsl and xslx format.

To read a file just instanciate a new XSSFWorkbook by passing a new FileInputStream with the path of the Excel file:

XSSFWorkbook workbook = new XSSFWorkbook(OPCPackage.open(new File("foo.xslx")));

Or with an input stream (takes a little more memory than a file):

XSSFWorkbook workbook = new XSSFWorkbook(myInputStream);

After having a XSSFWorkbook, use it to iterate through all the cell (example).

Download Apache POI 3.9 here

Using POI 3.8 and poi-ooxml-3.8, I've had success with something like this (i've not tried older versions):

InputStream is = //Open file, and get inputstream
Workbook workBook = WorkbookFactory.create(is);
int totalSheets = workBook.getNumberOfSheets();
for (int i = 0; i <= totalSheets - 1; i++) {
  Sheet sheet = workBook.getSheetAt(i);
  // Do something with the sheet
}

WorkbookFactory will automatically determine whether the file is the old XLS, or newer XLSX and return the correct version of Workbook. The rest of the code is just a sample of iterating through the sheets contained within.

Add following dependencies in your code.

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>

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

Also to read excel file use the following code, it will work for both .xls as well as .xlsx file.

Workbook workbook = WorkbookFactory.create(inputStream);

Add dependencies in pom.xml -

<dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.15</version>
    </dependency>

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

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

Excel 2007 or later (.xlsx) - sample code -

 //reading data from byte array
            OPCPackage pkg = OPCPackage.open(new ByteArrayInputStream(data));
            Workbook wb = new XSSFWorkbook(pkg);
Sheet sheet = wb.getSheetAt(0);
            Iterator<Row> rows = sheet.rowIterator();

            while (rows.hasNext()) {
                int j = 5;
                Person person= new Person ();
                Row row = rows.next();
                if (row.getRowNum() > 0) {
                    person.setPersonId((int)(row.getCell(0).getNumericCellValue()));
                    person.setFirstName(row.getCell(1).getStringCellValue());
                    person.setLastName(row.getCell(2).getStringCellValue());
                    person.setGroupId((int)(row.getCell(3).getNumericCellValue()));
                    person.setUserName(row.getCell(4).getStringCellValue());
                    person.setCreditId((int)(row.getCell(5).getNumericCellValue()));
                }

            }

2)

        //reading from a file 
        File file = new File(pathxlsx);
        FileInputStream fis = new FileInputStream(file);
        Workbook wb = new XSSFWorkbook(fis);

        Sheet sheet = wb.getSheetAt(0);
        Iterator<Row> rows = sheet.rowIterator();

        while (rows.hasNext()) {
            int j = 5;
            Person person= new Person ();
            Row row = rows.next();
            if (row.getRowNum() > 0) {
                person.setPersonId((int)(row.getCell(0).getNumericCellValue()));
                person.setFirstName(row.getCell(1).getStringCellValue());
                person.setLastName(row.getCell(2).getStringCellValue());
                person.setGroupId((int)(row.getCell(3).getNumericCellValue()));
                person.setUserName(row.getCell(4).getStringCellValue());
                person.setCreditId((int)(row.getCell(5).getNumericCellValue()));
            }

        }

Excel 1998-2003 file (.xls) - you may use HSSF library. just use : Workbook wb = new HSSFWorkbook(pkg);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!