
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 need to use existing Excel data in your application and want an easy way to read a particular cell. This article explains how to get a specific cell value in Excel using Java and shows how to read a cell value as a string with or without formatting.
The following points are covered:
Java Excel reader library - installation
In this section, we discuss the procedure for the Java Excel reader library. If you use Maven, add the following configuration to your 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 commands:
mvn clean
mvn install -U
For other Java frameworks, 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. Each cell is identified by the intersection of a row number and a column letter, so note the cell reference before coding. Suppose we need the value of cell “G5”.
To read the cell value, follow these steps:
- Load the Excel file using the Workbook class.
- Get the reference of the desired cell in a Worksheet object with
Worksheet.getCells().get(String cellName)21. - Read and display the cell value using
Cell.getValue()20.
The code sample below shows how to retrieve and display an Excel cell value in Java.
Get an Excel cell value with formatting in Java
So far we have shown how to read a particular Excel cell value using the Java Excel reader library. Next, we describe how to read the string value of a cell with or without formatting.
- Load the Excel file using the Workbook class.
- Get the reference of the desired cell in a Worksheet object with
Worksheet.getCells().get(String cellName)21. - Print the cell value with style using
Cell.getStringValue(CellValueFormatStrategy.CELL_STYLE)22. - Optionally, print the cell value without style using
Cell.getStringValue(CellValueFormatStrategy.NONE)22.
The snippet below shows how to print a cell value with or without formatting in Java. If the cell contains “0.12345”, the output is:
Value with cell style: 0.12
Value without any formatting: 0.12345
Get a free license
You can obtain a free temporary license to try the API without evaluation limitations.
Conclusion
We hope you now know how to retrieve the value of a specific Excel cell in Java using an Excel reader library. You also learned how to get the cell value as a string with or without formatting.
Visit the documentation to learn more about the Java Excel reader API.
Stay tuned at conholdate.com for regular updates.
Ask a question
Post your questions or queries on our forum.