如何使用Apache POI 3.14创建和编辑密码保护Excel工作表?

时间:2016-07-27 13:10:45

标签: java excel passwords apache-poi protected

嘿伙计们我正在研究将数据记录到受密码保护的Excel工作表的Swing应用程序。

我最初的问题是我无法找到有关如何从头开始创建带有密码保护的Excel工作表的正确文档,而且我不完全确定Apache POI版本3.14是否支持它。任何有关此事的见解都将受到高度赞赏。

然而,我真正的问题是假设我已经有一个受密码保护的.xlsx文件(通过在Excel本身内手动设置密码),我可以通过WorkbookFactory.create(new FileInputStream(dataFile), "password");访问该文件但是一旦代码执行了,该文件不再受密码保护,现在任何人都可以访问它。

这是我的代码片段:

// Sheet 1
private void logSheet1(File dataFile) throws IOException, InvalidFormatException {
    Workbook workBook = WorkbookFactory.create(new FileInputStream(dataFile), "password");
    Sheet sheet1 = workBook.getSheet("Sheet 1");
    Row row = sheet1.createRow(sheet1.getLastRowNum()+1);

    // data
    for(int i=0; i<log.length; i++) {
        if(log[i] == null) log[i] = new String("No data");
        Cell cell = row.createCell(i);
        cell.setCellType(Cell.CELL_TYPE_STRING);
        cell.setCellValue(log[i]);
    }

    FileOutputStream fos = new FileOutputStream(dataFile);
    workBook.write(fos);
    fos.close();
}


// Sheet 2
private void logSheet2(File dataFile) throws IOException, InvalidFormatException {
    Workbook workBook = WorkbookFactory.create(new FileInputStream(dataFile), "password");
    Sheet sheet2 = workBook.getSheet("Sheet 2");
    Row row = sheet2.createRow(sheet2.getLastRowNum()+1);

    // data
    for(int i=0; i<log.length; i++) {
        if(log[i] == null) log[i] = new String("No data");
        Cell cell = row.createCell(i);
        cell.setCellType(Cell.CELL_TYPE_STRING);
        cell.setCellValue(log[i]);
    }

    FileOutputStream fos = new FileOutputStream(dataFile);
    workBook.write(fos);
    fos.close();
}

1 个答案:

答案 0 :(得分:0)

是的,Apache POI支持Excel中的密码保护,编程也非常简单。

以下是使用JAVA中的Apache POI保护Excel的示例代码,希望它能为您提供帮助。

package excel_encryptor;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.poifs.crypt.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
//apache poi imports
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Encryption {

    public static void main(String[] args) throws IOException, InvalidFormatException, GeneralSecurityException {

        //create a new workbook
        Workbook wb = new XSSFWorkbook();

        //add a new sheet to the workbook
        Sheet sheet1 = wb.createSheet("Sheet1");

        //add 2 row to the sheet
        Row row1 = sheet1.createRow(0);
        Row row2 = sheet1.createRow(1);

        //create cells in the row
        Cell row1col1 = row1.createCell(0);
        Cell row1col2 = row1.createCell(1);

        //add data to the cells
        row1col1.setCellValue("Top Secret Data 1");
        row1col2.setCellValue("Top Secret Data 2");

        //write the excel to a file
        try {
            FileOutputStream fileOut = new FileOutputStream("D:/path/excel.xlsx");
            wb.write(fileOut);
            fileOut.close();
        } catch (IOException e) {
        }

        //Add password protection and encrypt the file
        POIFSFileSystem fs = new POIFSFileSystem();
        EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);
        Encryptor enc = info.getEncryptor();
        enc.confirmPassword("s3cr3t"); // s3cr3t is your password to open sheet.

        OPCPackage opc = OPCPackage.open(new File("D:/path/excel.xlsx"), PackageAccess.READ_WRITE);
        OutputStream os = enc.getDataStream(fs);
        opc.save(os);
        opc.close();

        FileOutputStream fos = new FileOutputStream("D:/path/excel.xlsx");
        fs.writeFilesystem(fos);
        fos.close();    

        System.out.println("File created!!");

    }
}