search
HomeJavajavaTutorialHow to use Java to encrypt and decrypt Excel files?

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

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

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

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

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

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

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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools