Java POI : How to read Excel cell value and not the formula computing it?

Cover Image for Java POI : How to read Excel cell value and not the formula computing it?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

šŸ“ Blog Post: Java POI: How to Read Excel Cell Value and Not the Formula Computing It

šŸ‘‹ Hey there, fellow developers! Are you using Apache POI API to extract values from an Excel file in your Java project? That's awesome! However, you may have encountered a bit of a hiccup when dealing with cells that contain formulas. Instead of getting the actual value of the cell, you're stuck with the formula itself šŸ˜•. Don't worry, we've got your back! In this blog post, we'll dive into a common issue faced by developers when working with Apache POI and provide you with easy solutions to overcome it.

The Problem

Let's say you have an Excel file with cells containing formulas, and you want to retrieve the computed values. You might be tempted to use the getStringCellValue() method provided by Apache POI to get the value. Unfortunately, this will only give you the formula itself, not the calculated result. So what can you do to get the desired outcome?

One solution you might come across is using the evaluateFormulaCell() method, which seems promising. However, there's a catch. If you're using Excel's GETPIVOTDATA formula, this method won't work. You'll end up facing a frustrating NotImplementedException šŸ˜«.

<pre><code>Exception in thread "main" org.apache.poi.ss.formula.eval.NotImplementedException: Error evaluating cell Landscape!K11 at org.apache.poi.ss.formula.WorkbookEvaluator.addExceptionInfo(WorkbookEvaluator.java:321) at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:288) at org.apache.poi.ss.formula.WorkbookEvaluator.evaluate(WorkbookEvaluator.java:221) at org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.evaluateFormulaCellValue(HSSFFormulaEvaluator.java:320) at org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.evaluateFormulaCell(HSSFFormulaEvaluator.java:213) at fromExcelToJava.ExcelSheetReader.unAutreTest(ExcelSheetReader.java:193) at fromExcelToJava.ExcelSheetReader.main(ExcelSheetReader.java:224) Caused by: org.apache.poi.ss.formula.eval.NotImplementedException: GETPIVOTDATA at org.apache.poi.hssf.record.formula.functions.NotImplementedFunction.evaluate(NotImplementedFunction.java:42) </code></pre>

The Solution

Fear not! We have a workaround for you. Instead of using getStringCellValue() or evaluateFormulaCell(), we can utilize the DataFormatter class provided by Apache POI to get the cell value as it appears in Excel.

Here's a code snippet to get you started:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;

public class ExcelReader {

    public static void main(String[] args) {
        try (FileInputStream file = new FileInputStream("path/to/your/excel/file.xlsx");
             Workbook workbook = new XSSFWorkbook(file)) {

            Sheet sheet = workbook.getSheetAt(0);
            Row row = sheet.getRow(0);
            Cell cell = row.getCell(0);

            DataFormatter dataFormatter = new DataFormatter();
            String cellValue = dataFormatter.formatCellValue(cell);

            System.out.println("Cell Value: " + cellValue);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

By using the DataFormatter class, we can ensure that we get the actual value of the cell, regardless of whether it contains a formula or not. This solution works smoothly, even with formulas like GETPIVOTDATA. šŸŽ‰

Conclusion

Now you know how to overcome the hurdle of retrieving the computed value from an Excel cell when using Apache POI. By adopting the approach of using DataFormatter, you can easily obtain the desired values instead of getting stuck with the formula itself.

So go ahead and implement this solution in your project, read those Excel values like a boss, and take your Java skills to the next level! šŸ’Ŗ

If you found this blog post helpful, we'd love to hear from you. Share your thoughts in the comments section below and let us know if you have any other Java or Apache POI related questions. Happy coding! šŸ˜„šŸš€


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

šŸ”„ šŸ’» šŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! šŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings šŸ’„āœ‚ļø Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide šŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? šŸ¤” Well, my

Matheus Mello
Matheus Mello