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>
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);
}
}
Top comments (0)