官网地址:C#/VB.Net Excel, Word, PowerPoint, PDF Component - Welcome to e-iceblue Company Ltd
Java集成Excel加解密示例
配置文件
文件枚举
- package com.boonya.spring.mybatis.plus.enums;
-
- import lombok.Getter;
-
- @Getter
- public enum FileType {
-
- EXCEL(".xls","Excel"),
- EXCEL_X(".xls","Excel X"),
- JSON(".json","JSON"),
- PDF(".pdf","PDF"),
- WORD(".doc","WORD"),
- WORD_X(".docx","WORD X"),
- ;
-
- private String type;
- private String desc;
-
- private FileType(String type,String desc){
- this.type = type;
- this.desc = desc;
- }
- }
抽象定义
- package com.boonya.spring.mybatis.plus.config;
-
- import com.boonya.spring.mybatis.plus.enums.FileType;
- import com.spire.xls.Workbook;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Configuration;
-
- import java.io.File;
-
- @Configuration
- public abstract class AbstractEncryptDecryptConfig {
-
- /**
- * 临时保存路径
- */
- @Value("${file.encrypt.save.path}")
- protected static final String TEMP_URL = "";
-
- /**
- * 加密保存路径
- */
- @Value("${file.encrypt.save.path}")
- protected static final String ENCRYPT_URL = "";
-
- /**
- * 解密保存路径
- */
- @Value("${file.decrypt.save.path}")
- protected static final String DECRYPT_URL = "";
-
- /**
- * 保存操作后的文件
- *
- * @param wb
- * @param targetPath
- */
- protected void save(Workbook wb, String targetPath) {
- File file = new File(targetPath);
- if (!file.exists()) {
- wb.saveToFile(targetPath);
- }
- }
-
- public String getEncryptFilePath(String fileName, FileType type) {
- return ENCRYPT_URL + File.separator + fileName + type.getType();
- }
-
- public String getDecryptFilePath(String fileName, FileType type) {
- return DECRYPT_URL + File.separator + fileName + type.getType();
- }
-
- /**
- * 加密
- *
- * @param fileName
- * @param securityPwd
- */
- public abstract void encrypt(String fileName, String securityPwd);
-
- /**
- * 解密
- *
- * @param fileName
- * @param securityPwd
- */
- public abstract void decrypt(String fileName, String securityPwd);
- }
加密解密
Workbook
- package com.boonya.spring.mybatis.plus.config;
-
- import com.boonya.spring.mybatis.plus.enums.FileType;
- import com.spire.xls.Workbook;
- import org.springframework.stereotype.Component;
- import java.io.File;
-
- @Component
- public class WorkbookEncryptDecrypt extends AbstractEncryptDecryptConfig {
-
- /**
- * 加密Excel
- *
- * @param fileName
- * @param securityPwd
- */
- public void encrypt(String fileName, String securityPwd) {
- //加载Excel文档
- Workbook wb = new Workbook();
- wb.loadFromFile(TEMP_URL + File.separator + fileName);
- //加密工作簿
- wb.protect(securityPwd);
- //保存文档
- String targetPath = getEncryptFilePath(fileName, fileName.contains(FileType.EXCEL.getType()) ? FileType.EXCEL : FileType.EXCEL_X);
- save(wb, targetPath);
- }
-
- /**
- * 解密Excel
- *
- * @param fileName
- * @param securityPwd
- */
- public void decrypt(String fileName, String securityPwd) {
- //加载文档
- Workbook wb = new Workbook();
- wb.setOpenPassword(securityPwd);
- wb.loadFromFile(ENCRYPT_URL + File.separator + fileName);
- //解除密码保护
- wb.unProtect();
- //保存文档
- String targetPath = getDecryptFilePath(fileName, fileName.contains(FileType.EXCEL.getType()) ? FileType.EXCEL : FileType.EXCEL_X);
- save(wb, targetPath);
- wb.dispose();
- }
- }
Worksheet
- package com.boonya.spring.mybatis.plus.config;
-
- import com.boonya.spring.mybatis.plus.enums.FileType;
- import com.spire.xls.FileFormat;
- import com.spire.xls.Workbook;
- import com.spire.xls.Worksheet;
- import org.springframework.stereotype.Component;
- import java.io.File;
- @Component
- public class WorkSheetEncryptDecrypt extends AbstractEncryptDecryptConfig{
-
- /**
- * 加密Excel
- * @param fileName
- * @param securityPwd
- */
- public void encrypt(String fileName,String securityPwd){
- //加载Excel文档
- Workbook wb = new Workbook();
- wb.loadFromFile(TEMP_URL + File.separator + fileName);
- //获取工作表,加密
- Worksheet sheet = wb.getWorksheets().get(0);
- sheet.protect(securityPwd);
- //保存文档
- String targetPath = getEncryptFilePath(fileName ,fileName.contains(FileType.EXCEL.getType()) ? FileType.EXCEL : FileType.EXCEL_X);
- wb.saveToFile(targetPath, FileFormat.Version2013);
- wb.dispose();
- }
-
- /**
- * 解密Excel
- * @param fileName
- * @param securityPwd
- */
- public void decrypt(String fileName,String securityPwd){
- //加载文档
- Workbook wb = new Workbook();
- wb.loadFromFile(ENCRYPT_URL + File.separator + fileName);
- //获取工作表
- Worksheet sheet = wb.getWorksheets().get(0);
- //解除工作表的密码保护
- sheet.unprotect(securityPwd);
- //保存文档
- String targetPath = getDecryptFilePath(fileName , fileName.contains(FileType.EXCEL.getType()) ? FileType.EXCEL : FileType.EXCEL_X);
- wb.saveToFile(targetPath);
- wb.dispose();
- }
- }