DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

How to protect Excel files in Java applications

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


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

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

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

Part 3: Lock some certain cells on the Excel Worksheet.

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

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.

Top comments (0)