Excel files are widely used to store and organize data, from personal finances to business records. To prevent the changes or sharing of important data in your Excel files, Spire.XLS for java offers many ways to protect your Excel files. This article will show you how to protect the MS Excel files in Java applications from the following four parts.
- Set Password to Protect Excel Files using Java
- Mark as final
- Java Protect Excel Worksheets with a Specific Protection Type
- Add digital signature to Excel
Install Spire.XLS for Java
First of all, you're required to add the 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 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</artifactId>
<version>13.4.1</version>
</dependency>
</dependencies>
Protect Excel Files using Java
Spire.XLS for java offers Workbook.protect() method to restrict the access to Excel spreadsheet with a password. This prevents unauthorized users from opening or modifying the file without the correct password. The following are the steps to password protect a workbook using Spire.XLS for Java.
- Create a Workbook object.
- Load an Excel file using Workbook.loadFromFile() method.
- Protect the workbook using a password using Workbook.protect() method.
- Save the workbook to another Excel file using Workbook.saveToFile() method.
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
public class passwordProtectWorkbook {
public static void main(String[] args) throws Exception {
//Create a Workbook object
Workbook workbook = new Workbook();
//Load an Excel file
workbook.loadFromFile("Sample.xlsx");
//Protect workbook with a password
workbook.protect("psd123");
//Save the workbook to another Excel file
workbook.saveToFile("Encrypted.xlsx", ExcelVersion.Version2016);
}
}
Mark as Final
The Mark as Final command makes the Excel workbook read-only and prevents changes to the document. When you share an Excel that is marked as final, you're telling viewers the spreadsheet is final and no changes on it are wanted.
- Create a Workbook object.
- Load an Excel file using Workbook.loadFromFile() method.
- Set the document property MarkAsFinal as true
- Save the workbook to another Excel file using Workbook.saveToFile() method
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
public class markAsFinal {
public static void main(String[] args) throws Exception {
//Create a Workbook object and load the sample file
Workbook workbook = new Workbook();
workbook.loadFromFile("Sample.xlsx");
//Set the document property MarkAsFinal as true
workbook.getCustomDocumentProperties().add("_MarkAsFinal", true);
//Save the workbook to another Excel file
workbook.saveToFile("Final.xlsx", ExcelVersion.Version2016);
}
Protect Worksheets using Java
If you wish to grant people permission to read your Excel document but restrict the types of modifications they are allowed to make on a worksheet, you can protect the worksheet with a specific protection type. Here are the steps:
- Create a Workbook object.
- Load an Excel file using Workbook.loadFromFile() method.
- Get a specific worksheet using Workbook.getWorksheets().get(index) method.
- Protect the worksheet with a protection type using Worksheet.protect(String password, EnumSet.of options) method.
- Save the workbook to another Excel file using Workbook.saveToFile() method.
import com.spire.xls.ExcelVersion;
import com.spire.xls.SheetProtectionType;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import java.util.EnumSet;
public class protectWorksheet {
public static void main(String[] args) throws Exception {
//Create a Workbook object
Workbook workbook = new Workbook();
workbook.loadFromFile("Sample.xlsx");
//Get a specific worksheet
Worksheet worksheet = workbook.getWorksheets().get(1);
//Protect the worksheet with the permission password and the specific protect type
worksheet.protect("psd-permission", EnumSet.of(SheetProtectionType.None));
//Save the workbook to another Excel file
workbook.saveToFile("ProtectWorksheet.xlsx", ExcelVersion.Version2016);
}
}
Add digital signatures to Excel
You can add a digital signature to verify the authenticity of an Excel file. Once the digital signature is added, the file becomes read-only to discourage further editing. If someone makes changes to the file, the digital signature will become invalid immediately.
- Instantiate a Workbook instance.
- Load an Excel file using Workbook.loadFromFile() method.
- Instantiate a CertificateAndPrivateKey instance with the specified certificate (.pfx) file path and the password of the .pfx file.
- Add a digital signature to the file using Workbook.addDigitalSignature(CertificateAndPrivateKey, String, Date) method.
- Save the result file using Workbook.saveToFile() method.
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.digital.CertificateAndPrivateKey;
import java.util.Date;
public class addDigitalSignature {
public static void main(String[] args) throws Exception {
//Create a Workbook object
Workbook workbook = new Workbook();
workbook.loadFromFile("Sample.xlsx");
//Add a digital signature to the file
CertificateAndPrivateKey cap = new CertificateAndPrivateKey("test.pfx","e-iceblue");
workbook.addDigitalSignature(cap, "e-iceblue",new Date());
//Save the result file
workbook.saveToFile("AddDigitalSignature.xlsx", ExcelVersion.Version2013);
}
}
Conclusion:
In this article, you have learned how to protect Excel Spreadsheets using Java by password protection, read-only access, worksheet protection, and digital signatures. Furthermore, Spire.XLS for Java also supports to unprotect the Excel workbook nor a particular worksheet in an Excel workbook. You can explore more about Spire.XLS for Java in Excel Forum.
Top comments (0)