简体   繁体   English

Apache POI - 如何使用日期单元格对 excel 进行密码保护?

[英]Apache POI - How to password-protect excel with date cells?

I'm using Apache POI to create password-protected excel, but it cannot work if I use the following:我正在使用 Apache POI 创建受密码保护的 excel,但如果我使用以下内容,它将无法工作:

cell = row.createCell(colNum);
cell.setCellValue(value);
var cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd/mm/yyyy"));
cell.setCellStyle(cellStyle)

The date values are number of days since 01/01/1900.日期值是自 1900 年 1 月 1 日以来的天数。

If I omit Date Formatting - the encryption works fine.如果我省略日期格式 - 加密工作正常。 Has anyone managed to have created a password-protected excel with date cells?有没有人设法用日期单元创建受密码保护的 excel?

With credit to Axel Richter , the answer was solved at this question styles.xml breaking password-protected XSSFWorkbook (Apache POI v3.16) save process .感谢Axel Richter ,答案在这个问题styles.xml 破坏密码保护的 XSSFWorkbook (Apache POI v3.16) save process 中得到了解决。

Note: This code is for server-side Javascript - using Mozilla Rhino v1.7R3 which provides support for nearly all of ECMAScript Edition 5 plus a few features from Mozilla Javascript 1.8.注意:此代码适用于服务器端 Javascript - 使用 Mozilla Rhino v1.7R3,它支持几乎所有 ECMAScript Edition 5 以及 Mozilla Javascript 1.8 中的一些功能。

To create password-protected excel with date cells, the format of date cell are created once, not at each cell creation.要使用日期单元格创建受密码保护的 excel,日期单元格的格式会创建一次,而不是在每次创建单元格时创建。


importPackage(org.apache.poi.xssf.usermodel);
importPackage(org.apache.poi.ss.usermodel);
importPackage(org.apache.poi.ss.util);
importPackage(org.apache.poi.poifs.filesystem);
importPackage(org.apache.poi.poifs.crypt);
importPackage(org.apache.poi.openxml4j.opc);

//Create the workbook
var wb = new XSSFWorkbook();

//Add styling at the top
var createHelper = wb.getCreationHelper();
var dateStyle = wb.createCellStyle();
dateStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd/mm/yyyy"));

//Process the cells
//loop..
createDateCell(row, colNum++, tables.SHE_SOUTH.DOB.value, dateStyle);
//..end of loop

function createDateCell(row,colNum,value, cellStyle){
    var cell;
    if(value){
        cell = row.createCell(colNum, Cell.CELL_TYPE_NUMERIC);
        cell.setCellValue(value);
        cell.setCellStyle(cellStyle);
    }
    else
    {
        cell = row.createCell(colNum, Cell.CELL_TYPE_BLANK);
    }
    return cell;
}

//Write out the excel
var fileOut = new java.io.FileOutputStream(path_and_filename);
wb.write(fileOut);
fileOut.close();

//Begin encryption
var fs = new POIFSFileSystem();
var info = new EncryptionInfo(EncryptionMode.agile, CipherAlgorithm.aes192,
 HashAlgorithm.sha384, -1, -1, null);
var enc = info.getEncryptor();
enc.confirmPassword("password");

var opc = OPCPackage.open(new java.io.File(path_and_filename), PackageAccess.READ_WRITE);
var os = enc.getDataStream(fs);
opc.save(os);
opc.close();

var fos = new java.io.FileOutputStream(path_and_filename);
fs.writeFilesystem(fos);
fos.close();


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM