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.
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: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.
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.
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:
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.
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:
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:
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:
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!