Protect Excel files in Java applications

<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>
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("data.xlsx");

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

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

}
}
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("data.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);

}
}
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("data.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);

}
}

--

--

--

Sharing Java Code

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Newly-Introduced JUnit 5 Annotations and Classes. The @Disabled annotation

Adventures in GameDev with GameDevHQ! Day 8 — Simple Player Movement in Unity

A solution to boost Python speed 1000x times

Apache Spark — How it runs on a cluster?

Computer Science Basics to Check Out

Day 9 — Retrieve Data with HTTP Query and Json process data

How to create alphabetical pagination with Django?

How to setup Metabase on a Windows machine

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrew Wilson

Andrew Wilson

Sharing Java Code

More from Medium

Solution for “Not able to find Java executable or version” in Jenkins

What happen if you exceed the Byte Range in Java? (128 to -127)

How to install Java on Linux

Database connectivity with java 🤯, using mysql…