Workbook给excel设置密码
1、读取已经存在的excel生成一个新的加密的excel文件
应用依赖
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.xls.free</artifactId> <version>3.9.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies>
代码
package org.example; import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; import org.junit.Test; public class MyTest { public static void main(String[] args) { } @Test public void lockFile() { String url = "C:\\Users\\50649\\Desktop\\test\\5555_FDAC2024052100021.xlsx"; //加载测试文档 Workbook wb = new Workbook(); wb.loadFromFile(url); //使用密码加密工作簿 wb.protect("123456"); //保存文档 wb.saveToFile("C:/Users/50649/Desktop/test/ProtectWorkbook112.xlsx", ExcelVersion.Version2010); wb.dispose(); } @Test public void unLockFile(){ //加载文档 Workbook wb = new Workbook(); wb.setOpenPassword("123456");//源文档密码 wb.loadFromFile("ProtectWorkbook.xlsx"); //解除密码保护 wb.unProtect(); //保存文档 wb.saveToFile("UnprotectWb.xlsx"); wb.dispose(); } }