How to find and replace data in Excel in Java

Replacing data in Excel can significantly simplify data analysis processes. For instance, if you want to analyze sales data for a particular product, you might need to replace all occurrences of the product code with its corresponding name. This enables easier comprehension and interpretation of the data, allowing for more efficient analysis and decision-making. Free Spire.XLS for Java is a powerful library that allows developers to manipulate Excel files programmatically. In this article, we will explore how to find and replace data within an Excel spreadsheet. Whether you need to update specific values or make global changes, this library provides an efficient solution.

Setting Up the Environment

Method 1: Introduced manually. Download Free Spire.XLS for Java locally, unzip it, and find the Spire.Xls.jar file in the lib folder. Open the following interface in IDEA, and import the jar file in the local path into the Java program:

If you use Maven, you can easily import the JAR file in your application by adding the following code to your project’s pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls.free</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>
  • Create an instance of Workbook class.

  • Load an Excel file using Workbook.loadFromFile() method.

  • Get the desired worksheet using Workbook.getWorksheets().get() method.

  • Find the specific value in the worksheet using Worksheet.findAllString() method and replace the value of the cell with another value using CellRange.setText() method.

  • Set a background for the cell so you can easily find the updated cells using CellRange. getStyle().setColor() method.

  • Save the result file using Workbook.saveToFile() method.

Java Code

import com.spire.xls.CellRange;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

import java.awt.Color;

public class ReplaceData {
    public static void main(String[] args) {
        // Initialize an instance of the Workbook class
        Workbook workbook = new Workbook();
        // Load an Excel file
        workbook.loadFromFile("Test.xlsx");

        // Get the first worksheet
        Worksheet worksheet = workbook.getWorksheets().get(0);

        // Find the cells with the specific string value "Total" in the worksheet
        CellRange[] cells = worksheet.findAllString("Total", true, true);

        // Iterate through the found cells
        for (CellRange cell : cells) {
            // Replace the value of the cell with another value
            cell.setText("Sum");
            // Set a background color for the cell
            cell.getStyle().setColor(Color.YELLOW);
        }

        // Save the result file to a specific location
        workbook.saveToFile("ReplaceDataInWorksheet.xlsx", ExcelVersion.Version2016);
        workbook.dispose();
    }
}

Conclusion:

Replacing data in Excel is crucial for maintaining accuracy, consistency, and efficiency in data management and analysis. With the help of Free Spire.XLS for Java, developers can easily find and replace data programmatically, streamlining the process and reducing manual effort. By leveraging this library's capabilities, users can effectively update data values, formulas, and presentation elements within Excel spreadsheets, ensuring the integrity and usefulness of the data contained within them.