Java encrypt / decrypt Excel

Keywords: Java Excel Maven

Summary

When setting 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, you can set the content of the table to read-only, and you cannot edit the worksheet. In addition, you can also set protection for a specific area of the worksheet, that is, you can set the specified area to edit or hide the data formula to protect the data information source. When there is no need to set the document protection, the password protection can be revoked, that is, the document can be decrypted. Next, the java program will demonstrate the implementation of the above encryption and decryption methods.

Sample Outline

1. Excel Workbook

1.1 encrypt Workbook

1.2 decrypt Workbook

2. Excel worksheet

2.1 encryption worksheet

2.2 encryption worksheet specifies data range

2.3 set worksheet formula invisible

2.4 decrypt Excel worksheet

 

tool

Tools: free flame.xls for Java (free version)

Note 1: available through Download official website Jar package, and unzip the jar file in the lib folder into the java program; or maven warehouse Download Install the import.

Note 2: when setting worksheet protection here, you can support different types of protection (as shown in the figure below). This article takes selecting one as an example.

Java code example

[example 1] encrypt Workbook

import com.spire.xls.*;

public class ProtectWb {
    public static void main(String[] args) {
        //Load test document
        Workbook wb = new Workbook();
        wb.loadFromFile("sample.xlsx");

        //Encrypt Workbook with password
        wb.protect("123456");

        //Save document
        wb.saveToFile("ProtectWorkbook.xlsx", ExcelVersion.Version2010);
        wb.dispose();
    }
}

Workbook encryption result:

[example 2] decrypt Workbook

import com.spire.xls.*;

public class UnprotectWb {
    public static void main(String[] args) {
        //Loading documents
        Workbook wb = new Workbook();
        wb.setOpenPassword("123456");//Source document password
        wb.loadFromFile("ProtectWorkbook.xlsx");

        //Remove password protection
        wb.unProtect();

        //Save document
        wb.saveToFile("UnprotectWb.xlsx");
        wb.dispose();
    }
}

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

 

[example 3] encryption worksheet

import com.spire.xls.*;

import java.util.EnumSet;

public class ProtectSheet {
    public static void main(String[] args) {
        //Load test document
        Workbook wb = new Workbook();
        wb.loadFromFile("sample.xlsx");

        //Get first sheet
        Worksheet sheet = wb.getWorksheets().get(0);

        //Use password encryption protection
        sheet.protect("654321", EnumSet.of(SheetProtectionType.All));

        //Save document
        wb.saveToFile("ProtectWorksheet.xlsx", ExcelVersion.Version2010);
        wb.dispose();
    }
}

Worksheet encryption result:

 

[example 4] encryption worksheet specifies data range

import com.spire.xls.*;

import java.util.EnumSet;

public class ProtectRange {
    public static void main(String[] args) {
        //Load test document
        Workbook wb = new Workbook();
        wb.loadFromFile("sample.xlsx");

        //Get first sheet
        Worksheet sheet = wb.getWorksheets().get(0);

        //Password encryption worksheet
        sheet.protect("123654", EnumSet.of(SheetProtectionType.All));
        //Specify editable area
        sheet.addAllowEditRange("AllowEdit",sheet.getCellRange(3,1,11,8));

        //Save document
        wb.saveToFile("ProtectRange.xlsx", ExcelVersion.Version2010);
        wb.dispose();
    }
}

Specified area encryption result:

 

[example 5] hide worksheet formula

import com.spire.xls.*;

import java.util.EnumSet;

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

        //Get worksheet
        Worksheet sheet = wb.getWorksheets().get(0);

        //Set encryption
        sheet.protect("123", EnumSet.of(SheetProtectionType.All));
        //Hidden formula
        sheet.getAllocatedRange().isFormulaHidden(true);

        //Save document
        wb.saveToFile("HideFormula.xlsx",FileFormat.Version2010);
        wb.dispose();
    }
}

Set formula hide results:

 

[example 6] decrypt Excel worksheet

import com.spire.xls.*;

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

        //Get worksheet
        Worksheet sheet = wb.getWorksheets().get(0);

        //Remove password protection (source document password required)
        sheet.unprotect("654321");

        //Save document
        wb.saveToFile("UnprotectSheet.xlsx",ExcelVersion.Version2010);
        wb.dispose();
    }
}

 

Run the program to generate a document in which the specified worksheet is no longer protected.

 

(end of this paper)

Posted by slick101 on Fri, 07 Feb 2020 01:45:26 -0800