Home > Java > javaTutorial > How to use Java to encrypt and decrypt Excel files?

How to use Java to encrypt and decrypt Excel files?

PHPz
Release: 2023-05-08 14:58:08
forward
2439 people have browsed it

Overview

When setting up excel file protection, you can usually choose to encrypt and protect the entire workbook, and you need to enter a password when opening the file; or you can encrypt the specified worksheet, that is, set the table content to be read-only and cannot be Make edits to the worksheet. In addition, you can also set protection for specific areas of the worksheet, that is, set the specified area to be editable or hide data formulas to protect the source of data information. When there is no need to set document protection, the password protection can be revoked, that is, the document can be decrypted.

Sample Outline

1. ExcelWorkbook

1.1 Encrypted Workbook

1.2 Decryption Workbook Book

2. ExcelWorksheet

2.1 Encrypt the worksheet

2.2 Specify the data range of the encrypted worksheet

2.3 Set worksheet formulas to be invisible

2.4 Decrypt Excel worksheet

Tool

Tool:Free Spire.XLS for Java (Free Version)

Note 1: You can download the jar package through the official website, decompress it and import the jar file in the lib folder into the java program; or download and install it through the maven warehouse Import.

How to use Java to encrypt and decrypt Excel files?

Note 2:When setting worksheet protection here, multiple different types of protection can be supported (as shown below), This article uses choosing one of them as an example.

How to use Java to encrypt and decrypt Excel files?

Java code example

Example 1 Encrypted workbook

import com.spire.xls.*;

public class ProtectWb {
    public static void main(String[] args) {
        //加载测试文档
        Workbook wb = new Workbook();
        wb.loadFromFile("sample.xlsx");

        //使用密码加密工作簿
        wb.protect("123456");

        //保存文档
        wb.saveToFile("ProtectWorkbook.xlsx", ExcelVersion.Version2010);
        wb.dispose();
    }
}
Copy after login

Workbook encryption result:

How to use Java to encrypt and decrypt Excel files?

Example 2 Decrypt workbook

import com.spire.xls.*;

public class UnprotectWb {
    public static void main(String[] args) {
        //加载文档
        Workbook wb = new Workbook();
        wb.setOpenPassword("123456");//源文档密码
        wb.loadFromFile("ProtectWorkbook.xlsx");

        //解除密码保护
        wb.unProtect();

        //保存文档
        wb.saveToFile("UnprotectWb.xlsx");
        wb.dispose();
    }
}
Copy after login

Run the program and the generated workbook file will no longer be password protected.

Example 3 encrypted worksheet

import com.spire.xls.*;

import java.util.EnumSet;

public class ProtectSheet {
    public static void main(String[] args) {
        //加载测试文档
        Workbook wb = new Workbook();
        wb.loadFromFile("sample.xlsx");

        //获取第一个工作表
        Worksheet sheet = wb.getWorksheets().get(0);

        //使用密码加密保护
        sheet.protect("654321", EnumSet.of(SheetProtectionType.All));

        //保存文档
        wb.saveToFile("ProtectWorksheet.xlsx", ExcelVersion.Version2010);
        wb.dispose();
    }
}
Copy after login

Worksheet encryption result:

How to use Java to encrypt and decrypt Excel files?

Example 4 encrypted worksheet specified data range

import com.spire.xls.*;

import java.util.EnumSet;

public class ProtectRange {
    public static void main(String[] args) {
        //加载测试文档
        Workbook wb = new Workbook();
        wb.loadFromFile("sample.xlsx");

        //获取第一个工作表
        Worksheet sheet = wb.getWorksheets().get(0);

        //密码加密工作表
        sheet.protect("123654", EnumSet.of(SheetProtectionType.All));
        //指定可编辑的区域
        sheet.addAllowEditRange("AllowEdit",sheet.getCellRange(3,1,11,8));

        //保存文档
        wb.saveToFile("ProtectRange.xlsx", ExcelVersion.Version2010);
        wb.dispose();
    }
}
Copy after login

Specified area encryption result:

How to use Java to encrypt and decrypt Excel files?

Example 5 Set worksheet formula hiding

import com.spire.xls.*;

import java.util.EnumSet;

public class ProtectAndHideFormula {
    public static void main(String[] args) {
        //加载文档
        Workbook  wb = new Workbook();
        wb.loadFromFile("sample.xlsx");

        //获取工作表
        Worksheet sheet = wb.getWorksheets().get(0);

        //设置加密
        sheet.protect("123", EnumSet.of(SheetProtectionType.All));
        //隐藏公式
        sheet.getAllocatedRange().isFormulaHidden(true);

        //保存文档
        wb.saveToFile("HideFormula.xlsx",FileFormat.Version2010);
        wb.dispose();
    }
}
Copy after login

Set formula hiding result:

How to use Java to encrypt and decrypt Excel files?

Example 6 Decrypt Excel worksheet

import com.spire.xls.*;

public class UnprotectSheet {
    public static void main(String[] args) {
        //加载文档
        Workbook  wb = new Workbook();
        wb.loadFromFile("ProtectWorksheet.xlsx");

        //获取工作表
        Worksheet sheet = wb.getWorksheets().get(0);

        //解除密码保护(需输入源文档密码)
        sheet.unprotect("654321");

        //保存文档
        wb.saveToFile("UnprotectSheet.xlsx",ExcelVersion.Version2010);
        wb.dispose();
    }
}
Copy after login

Run the program, and in the generated document, the specified worksheet will no longer be protected.

The above is the detailed content of How to use Java to encrypt and decrypt Excel files?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template