How to Read Excel Files in Java

Read data in Excel files using Java

MS Excel files are largely used to keep tabular data in the form of worksheets. Often, the huge datasets are also maintained in Excel files. Therefore, you may come across the scenario where you need to read and fetch data from the worksheets in an Excel file. To achieve that, this article shows how to read an Excel file in Java. We will demonstrate how to read the whole Excel file or only a single worksheet programmatically.

Java Library to Read Excel Files

To read data from the worksheets, we will use Aspose.Cells for Java. It is a spreadsheet manipulation API to create Excel files from scratch. Moreover, it allows you to read and manipulate existing Excel files quite easily. The API can be downloaded from the downloads section or installed by adding the following Maven configurations in pom.xml.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-cells</artifactId>
    <version>21.11</version>
</dependency>

Read an Excel File in Java

In Excel files, the data is stored in cells where each cell is identified by its name (A1, B3, etc.) or the row and column index. These cells collectively form a worksheet and an Excel file is comprised of single or multiple worksheets. All the worksheets are contained in a workbook. So this was a brief overview of the structure of an Excel file. Let’s now proceed to read data from an Excel file.

The following are the steps to read an Excel file and print its data using Java.

The following code sample shows how to read an Excel file in Java.

The following is the output that we get in the console after running the code sample above.

Reading data in the worksheets of Excel file in Java
Reading an Excel File in Java

Read a Particular Worksheet in Excel in Java

You can also read only a particular worksheet in the Excel file by following the steps below.

The following code sample shows how to read data from a particular worksheet in Java.

Get a Free API License

You can use Aspose.Cells for Java without evaluation limitations by getting a free temporary license.

Conclusion

In this article, you have learned how to read Excel files dynamically in Java. With the help of code samples, you have seen how to read data from a particular worksheet or all the worksheets in an Excel workbook. In addition, you can explore other features of Aspose.Cells for Java using the documentation. In case you would have any queries, feel free to let us know via our forum.

See Also