In the process of operating Excel documents, we can share them with others for collaborative modification, and use Tracked Changes to track, maintain these changes. If you have the full authority over the document, you can accept or reject each change. This tutorial will show you how to accept or reject all tracked changes using Java codes with the help of Spire.XLS for Java.
About Spire.XLS for Java
Spire.XLS
for Java is a professional Java Excel
API that enables developers to create, manage, manipulate, convert and
print Excel worksheets without using Microsoft Office or Microsoft Excel.
Install Spire.Xls.jar
First
of all, you need to add Spire.Xls.jar file as a dependency in your Java
program. The JAR file can be downloaded from this link. If you use Maven, you
can easily import the JAR file by adding the following code to your project's
pom.xml file.
<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</artifactId>
<version>4.9.0</version>
</dependency>
</dependencies>
Accept or Reject Tracked Changes in a Workbook
Spire.XLS for Java provides Workbook.hasTrackedChanegs() method to detect whether a workbook
has tracked changes. If yes, you can use Workbook.acceptAllTrackedchanges() method to accept all
changes. On the contrary, you can reject them using Workbook.rejectAllTrackedChanges() method.
The full code sample can be found as below.
import com.spire.xls.*;
public class TrackChanges {
public static void main(String[] args) {
//Create a Workbook object
Workbook wb = new Workbook();code
//Load the sample Excel file
wb.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.xlsx");
//Detect if the workbook has tracked changes
if (wb.hasTrackedChanges())
{
//Accept tracked changes in the workbook
wb.acceptAllTrackedChanges();
//Reject tracked changes in the workbook
//wb.rejectAllTrackedChanges();
}
//Save to file
wb.saveToFile("output/AcceptTrackedChanges.xlsx", FileFormat.Version2013);
}
}
No comments:
Post a Comment