How to get a specific cell value in Excel using Java

How to get a specific cell value in Excel using Java

Excel (XLSX or XLS) workbooks are widely used by large to small-scale organizations for financial data analysis, record keeping, and organizing data. An Excel file consists of one or multiple spreadsheets, and a sheet contains a grid of cells. As a Java developer, you might have a requirement to use the existing Excel file data in your application, and you are finding an easy way to read the value of a particular cell. To deal with it, we will discuss how to get a specific cell value in Excel using Java. Furthermore, we will see how to read a cell value as a string with or without formatting.

The following points will be covered in this article:

Java Excel reader library - installation

In this section, we will discuss the procedure of this Java Excel reader library. If you are using Maven to manage a Java project or app, add the following configurations in the pom.xml file.

<repository>
	<id>ConholdateJavaAPI</id>
	<name>Conholdate Java API</name>
	<url>https://releases.conholdate.com/java/repo</url>
</repository>        
<dependency>
	<groupId>com.conholdate</groupId>
	<artifactId>conholdate-total</artifactId>
	<version>23.6</version>
	<type>pom</type>
</dependency>

Then run the following commands.

mvn clean    
mvn install -U

For configurations about other Java frameworks, please visit releases.conholdate.com.

Read a specific Excel cell value using Java reader

This section demonstrates how to get a specific cell value in Excel worksheet. Since each cell’s location is identified by the reference of an intersection of row number and column letter, the cell’s identity should be kept in mind before opening the code editor. Suppose we need to get the value of the “G5” cell.

To read the cell value, follow the steps given below:

  1. Load Excel file using Workbook class.
  2. Obtain the reference of desired cell in a Worksheet object using Worksheet.getCells().get(String cellName) method.
  3. Read and display cell value using Cell.getValue() method.

The following code sample shows how to get and display an Excel cell value in Java.

Get an Excel cell value with formatting in Java

So far, we have explained how to read a particular Excel cell value using the Java Excel reader library. By adding more, we will describe how to read the string value of a cell with or without formatting. Please follow the steps given below:

  1. First, load the Excel file using Workbook class.
  2. Then, get the reference of desired cell in a Worksheet object using Worksheet.getCells().get(String cellName) method.
  3. Finally, print cell value with the applied style using Cell.getStringValue(CellValueFormatStrategy.CELL_STYLE) method.
  4. Optionally, you can print cell value without style using Cell.getStringValue(CellValueFormatStrategy.NONE) method.

The following code snippet shows how to get and print a cell value with or without formatting in Java. Suppose “0.12345” is a cell value, the output will be displayed as follows:

Value with cell style: 0.12
Value without any formatting: 0.12345

Get a free license

You can get a free temporary license to try the API without evaluation limitations.

Conclusion

To conclude, we hope you have learned how to get the value of a particular cell in Excel in Java using an Excel reader library. Furthermore, you have gone through how to obtain the cell value as a string with or without cell formatting.

You may visit the documentation to learn more about the Java Excel reader API.

Finally, please stay tuned at conholdate.com for regular updates.

Ask a question

You can let us know your questions or queries on our forum.

See also