×

Read the XLSX file in Java

Excel files contain cells; reading an excel file in Java differs from reading a word file. JDK doesn't have a direct API that can read or write Word or Excel documents. We must rely on Apache POI, a third-party library. t has numerous features, is widely used, and receives strong community support.

Apache POI

A Java API called Apache POI can read and write Microsoft Documents in both.xls and.xlsx format. Classes and interfaces are included.

For reading excel files, the Apache POI library offers two implementations:

  • Implementation of the HSSF (Horrible Spreadsheet Format) designates an API compatible with Excel 2003 or previous versions.
  • Implementation of XSSF (XML Spreadsheet Format): Indicates that an API is compatible with Excel 2007 or later versions.

Class and Interface used in Apache POI used for reading xlsx files

Classes

  • XSSFWorkbook is a class that symbolizes an XLSX file.
  • An XLSX file's sheet is represented by the class known as XSSFSheet.
  • The class XSSFRow stands for a row in an XLSX file's sheet.
  • The class XSSFCell represents a cell in a row of an XLSX file.

Interface

  • Workbook
    It serves as a representation of an Excel workbook. The workbook is an interface that the book and XSSFWorkbook have implemented. This Interface or sheet represents a worksheet in Excel. A workbook's main component, the sheet, represents a grid of cells. Java.lang.The Sheet interface extends Iterable.
  • Row
    It is a separate interface that depicts a spreadsheet row. Java.lang.The Row interface extends Iterable. The HSSFRow and XSSFRow concrete classes exist.
  • Cell
    It serves as an interface. Implementing Cell interfaces are HSSFCell and XSSFCell.

    Program for reading xlsx file in Java

ReadXLSX.java

// importing all necessary packages
import Java. io. File ;
import java. io . FileInputStream ;
import java . io . IOException ;
import org . apache . poi . hssf . usermodel . HSSFSheet ;
import org . apache . poi . hssf . usermodel . HSSFWorkbook ;
import org . apache . poi . ss . usermodel . Cell ;
import org . apache . poi . ss . usermodel . FormulaEvaluator ;
import org . apache . poi . ss . usermodel . Row ;
// Creating the main class with name ReadXLSX
public class ReadXLSX {
// Main section where execution starts
public static void main ( String [ ] args )
{
// using try block to prevent the stoppage of execution due to errors
try {
// creating object for the class FileInputStream to take the input from user i .e; xlsx file
FileInputStream f1 = new FileInputStream ( new File ( " example.xlsx " ) ) ;
// creating instance for workbook
XSSFWorkbook workbook1 = new XSSFWorkbook ( f1 ) ;
// creating instance for the class XSSFSheet
XSSFSheet st = workbook1 . getSheetAt ( 0 ) ;
// Using Iterator to iterate through the excel sheet
Iterator < Row > r = st . iterator ( ) ;
// Iterating over each row od the excel sheet
while ( r . hasNext ( ) ) {
// creating variable with name row for Row keyword to iterate through the sheet
Row row = r . next ( ) ;
Iterator < Cell > c = row . cellIterator ( ) ;
// Itearator to iterate each column
while ( c . hasNext ( ) ) {
Cell cell = c . next ( ) ;
// using the switch case 
switch ( cell . getCellType ( ) ) 
{
case Cell . CELL_TYPE_NUMERIC :
System . out . print ( cell . getNumericCellValue ( ) + "t" ) ;
break ;
// if the cell contains string value
case Cell . CELL_TYPE_STRING :
System . out . print ( cell . getStringCellValue ( ) + ) ;
break ;
}
}
System . out . println ( " " ) ;
}
// open excel file should be closed
f1 . close ( ) ;
}
// if Exception is raised, it is caught here 
// Exception is the main class which contains all the predefined exceptions in it
catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
}

Output

Read the XLSX file in Java

Related Topics

Sorting a String in Java

Sorting a String in Java Sorting is a process of arranging available data objects in a meaningful format that is ascending or descending order. Sorting a string or array of String...

4 minutes read.

HashMap Vs HashTable

HashMap HashMap is the basic implementation of the map interface in Java. HashMap stores the data in key and value pairs. Keys are used to access the value of the element. It...

5 minutes read.

Maximizing Profit in Stock Buy Sell in Java

In this tutorial, we will deal with a popular problem, a favourite of interviewers. The problem is named as Maximising profit in stock Buy Sell. we will see certain approaches...

6 minutes read.

Default Virtual Behaviour in C++ vs Java

Virtual Behaviour in C++: The class member methods in C++ are, by default, non-virtual. This implies that by simply defining it, they can be turned into virtual. The virtual class can be...

3 minutes read.

Instanceof operator in Java

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). Because it compares the instance with type, the...

3 minutes read.

Graph Problems in Java

A graph is a special type of data structure used in programming that is made up of edges connecting each set of finitely many nodes, or vertices, to each other....

14 minutes read.

How to reverse a linked list in java

The process of reversing a linked list in Java will be covered in this section. One of the most common questions in interviews is about reversing a linked list. If...

11 minutes read.

Thread Synchronization in Java

In Java, the smallest processing component is a thread, which is a small subprocess. It follows a different course of action. Threads are autonomous. If an exception occurs in one thread,...

6 minutes read.

Sparse Numbers in Java

In this section, we will be very well acknowledged about the sparse numbers in Java, how a number can be verified if it is a sparse number or not. Sparse Numbers Any...

3 minutes read.

Economical number in Java

Economical number is said to be economically efficient if the number of digits after prime factorization of the original number with their powers is less than the number of digits...

3 minutes read.

Count of Range Sum Problem in Java

In this article, we will discuss the basic approach or native approach used to count range sum problem in java. To solve this problem, we will check for numbers if they...

6 minutes read.

Java Xml Parser

In this article, we acknowledge you about what is a XML parser in Java, how is it going to work and what is the purpose of it. Also, the article discusses...

6 minutes read.

Polymorphism Program in Java

Polymorphism Program in Java: Polymorphism means the existence of different forms of an object. The object can be a class object or a real-time entity. For example, a person can...

5 minutes read.

Upcasting and Downcasting in Java

Type casting in Java is an important and very interesting topic to deal with. But here upcasting and downcasting is somewhat related to typecasting. In normal typecasting, we convert from...

6 minutes read.

Menu Driven Program in Java

Menu Driven Program in Java The menu-driven program in Java is a program that displays a menu and then takes input from the user to choose an option from the displayed...

3 minutes read.

Difference between final, finally, and finalize()

Difference between final, finally, and finalize() In Java, final, finally and finalize sound similar they are totally different in functionality. Final and finally are the two keywords but the finalize is...

4 minutes read.

Intersection Point of two linked list in Java

In this article, you will be very well acknowledged about how to achieve the intersection point of two linked list in Java. There are several approaches to obtain. Surely each...

8 minutes read.

Java Anon Proxy

The Java Anon Proxy (JAP), also known as JonDonym, is a proxy system designed to enable Web browsing with revocable (the use of or publication under a pseudonym, a false...

4 minutes read.

Java Comparable Interface

We implement comparable interface when we need a new logic for determining equality. if two objects are equal, the equals() method returns true and compareTo() method returns 0. The basic...

1 minute read.

Java String Methods

Java String Methods Java String class is the most important class of the java.lang package. It is used to handle the String related operations. It contains a lot of built-in Java...

2 minutes read.