DEV Community

CodeSharing
CodeSharing

Posted on

Java Rename Excel Worksheets and Set Tab Color

Usually an Excel document may contain several worksheets with similar names like Sheet1, Sheet2, Sheet3, etc. In order to facilitate our search and operation, we can rename these worksheets and set different tab colors by using Free Spire.XLS for Java.

Import Jar Dependency
Method 1: Download the Free Spire.XLS for Java and unzip it. Then add the Spire.Xls.jar file to your project as dependency.

Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls.free</artifactId>
        <version>3.9.1</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Java Code

import com.spire.xls.*;
import java.awt.*;

public class RenameSheetandSetTabColor {
    public static void main(String[] args) {
        //Load the Word document
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Input.xlsx");

        //Rename the first worksheet and set its tab color
        Worksheet worksheet = workbook.getWorksheets().get(0);
        worksheet.setName("Cost");
        worksheet.setTabColor(Color.YELLOW);

        //Rename the second worksheet and set its tab color
        worksheet = workbook.getWorksheets().get(1);
        worksheet.setName("Income");
        worksheet.setTabColor(Color.GREEN);

        //Rename the third worksheet and set its tab color
        worksheet = workbook.getWorksheets().get(2);
        worksheet.setName("Profit");
        worksheet.setTabColor(Color.RED);

        //Save the document to file
        workbook.saveToFile("output/Rename.xlsx", ExcelVersion.Version2010);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Sentry image

Make it make sense

Only get the information you need to fix your code that’s broken with Sentry.

Start debugging →

Top comments (0)

DevCycle image

Ship Faster, Stay Flexible.

DevCycle is the first feature flag platform with OpenFeature built-in to every open source SDK, designed to help developers ship faster while avoiding vendor-lock in.

Start shipping

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay