· 

Java – Password Protect an Excel Workbook or Worksheet

The protection level to an Excel file may be different in different situations. For instance, you may encrypt a whole workbook with an open password so people having no rights to access it are not able to read it, you may protect a specific sheet in a workbook so that the worksheet is accessible but read only. Also, there could be the case that you want some cells inside a worksheet are editable or fillable while the others are forbidden from editing.

 

To perform the above-mentioned tasks programmatically, this article shows how to password protect workbooks or worksheets using Free Spire.XLS for Java.

Installing Spire.Xls.jar

If you create a Maven project, you can easily import the jar in your application using the following configurations. For non-Maven projects, download the jar file from this link and add it as a dependency in your application.

<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.free</artifactId>
        <
version>3.9.1</version>
    </
dependency>
</
dependencies>

Example 1. Protect a workbook

import com.spire.xls.Workbook;
import com.spire.xls.ExcelVersion;

public class ProtectWorkbook {

   
public static void main(String[] args) {

       
//Create a Workbook object
       
Workbook workbook = new Workbook();

       
//Load the sample Excel file
       
workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx ");

       
//Protect the workbook with a password
       
workbook.protect("abc-123");

       
//Save to file
       
workbook.saveToFile("ProtectWorkbook.xlsx", ExcelVersion.Version2016
);
    }
}

Example 2. Protect a worksheet

import com.spire.xls.Worksheet;
import com.spire.xls.Workbook;
import com.spire.xls.ExcelVersion;
import com.spire.xls.SheetProtectionType;

import java.util.EnumSet;

public class ProtectWorksheet {

   
public static void main(String[] args) {

       
//Create a Workbook object
       
Workbook workbook = new Workbook();

       
//Load the sample Excel file
       
workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx ");

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

       
//Protect the worksheet with a password
       
sheet.protect("abc-123", EnumSet.of(SheetProtectionType.All));

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

Example 3. Protect selected cell ranges in a worksheet

import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import com.spire.xls.ExcelVersion;
import com.spire.xls.SheetProtectionType;

import java.util.EnumSet;

public class ProtectCellRange {

   
public static void main(String[] args) {

       
//Create a Workbook object
       
Workbook workbook = new Workbook();

       
//Load the sample Excel file
       
workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");

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

       
//Unprotect a certain cell range, which means the remaining cells will be protected
       
sheet.getCellRange("C4:F6").getCellStyle().setLocked(false);

       
//Set the password
       
sheet.protect("abc-123", EnumSet.of(SheetProtectionType.All));

       
//Save to file
       
workbook.saveToFile("ProtectCellRanges.xlsx", ExcelVersion.Version2010
);
    }
}

Write a comment

Comments: 0