Java Protect Excel Files
Tags: java, protect, encrypted, excel
Published: 2023-04-25 08:59:26

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](https://www.e-iceblue.com/Introduce/xls-for-java.html) 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] (#protect-excel-files-using-java)
- [Mark as final] (#mark-as-final)
- [Java Protect Excel Worksheets with a Specific Protection Type] (#protect-worksheets-using-java)
- [Add digital signature to Excel] (#add-digital-signatures-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](https://www.e-iceblue.com/Download/xls-for-java.html). 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.

```java
<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.

```java
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);
    }
}
```

![Protect workbook with password](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f4f133pnb6r8u711hvqp.png)

###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

```java
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);
    }
```


![Mark as Final](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aamcbce0egmcd71lix76.png)

###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 <SheetProtectionType> options)** method.
- Save the workbook to another Excel file using **Workbook.saveToFile()** method.

```java
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);
    }
}
```

![Protect worksheet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nx1840qmr33mt0ezu7t7.png)

###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.

```java
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);
    }
}
```

![Add digital signature](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xftq3dq4o6huo287btfw.png)

###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](https://www.e-iceblue.com/forum/spire-xls-f4.html).