How to protect Excel files in Java applications
Tags: javaexcel, protectexcelworkbook, encrypteexcelworksheet
Published: 2020-01-19 03:41:59

---
title: How to protect Excel files in Java applications
published: true
description: protect Excel files in Java applications
tags: java Excel, protect excel workbook, encrypte excel worksheet
---

The data security becomes more and more important since the internet spreads the data quickly than we can imagine. We always use Excel files to store a large amount of data which is very important to us in our daily life. So in this article, we are going to give you a detailed introduction about how to protect the excel files with password in Java.

This article will show you from the following three parts:

Encrypt the whole excel workbook with password in Java
Protect a certain worksheet with password in Java
Lock some certain cells on the worksheet in Java 

Part 1: Encrypt the whole excel workbook with password

```java

import com.spire.xls.*;

public class EncryptWorkbook {
    public static void main(String[] args) {
        //Create a workbook and load a file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx");

        //Protect the workbook with the password you want
        workbook.protect("eiceblue");

        //Save the Excel file
        workbook.saveToFile("output/EncryptWorkbook.xlsx", ExcelVersion.Version2010);
    }
}
```

Here is the effective screenshot when we open the protected workbook:
![Encrypted Excel workbook](https://thepracticaldev.s3.amazonaws.com/i/bwt8jx7e4qucfiuqafwx.png)

Part 2: Only protect the first worksheet on the excel workbook.

```java
import com.spire.xls.*;
import java.util.EnumSet;

public class ProtectWorksheet {
    public static void main(String[] args) {
        //Create a workbook and load a file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Protect this sheet with a password.
        sheet.protect("TestPassword", EnumSet.of(SheetProtectionType.All));

        //Save the Excel file
        workbook.saveToFile("output/ProtectWorksheet.xlsx", ExcelVersion.Version2010);

    }
}
```

Here is the screenshot of the protected worksheet:

![Protect Excel worksheet]
(https://thepracticaldev.s3.amazonaws.com/i/2uo8e6ux0nwmfvea0qt5.png)
 
Part 3: Lock some certain cells on the Excel Worksheet.

```java
import com.spire.xls.*;
import java.util.EnumSet;

public class ProtectCell {
    public static void main(String[] args) {
        //Create a workbook and load a file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Protect cell
        sheet.getCellRange("B3").getCellStyle().setLocked(true);
        sheet.getCellRange("C3").getCellStyle().setLocked(false);

        //Protect sheet
        sheet.protect("TestPassword", EnumSet.of(SheetProtectionType.All));

        //Save the Excel file
        workbook.saveToFile("output/ProtectCell.xlsx", ExcelVersion.Version2010);

    }
}
```
Here is the protected Excel cells:
![Lock Cells](https://thepracticaldev.s3.amazonaws.com/i/lrlbe652r63abti8sjmx.png)
 
Conclusion: 
Thanks to Free Spire.XLS, I could show you how to protect/encrypted the Excel workbook, Excel worksheets and certain Excel cells easily in java. Besides encrypted the Excel file formats with password, it also supports to unlock and decrypted the Excel files. Thanks for your reading.