style.xml 破解受密码保护的 XSSFWorkbook (Apache POI v3.16) 保存过程 [英] styles.xml breaking password-protected XSSFWorkbook (Apache POI v3.16) save process
问题描述
目前在 Java 版本 1.7.0-251 (Unix) 上使用 Apache POI 3.16
Currently using Apache POI 3.16 on Java version 1.7.0-251 (Unix)
从@Aniruddh Chandegra (如何使用 Apache POI 3.14 创建和编辑密码保护的 Excel 表格?)
Taking a leaf out of the example explained by @Aniruddh Chandegra (How to create and edit a password protect excel sheet using Apache POI 3.14?)
注意:我在服务器端 Javascript 上运行代码 - 使用 Mozilla Rhino v1.7R3,它支持几乎所有 ECMAScript 版本 5 以及 Mozilla Javascript 1.8 的一些功能.
Note: I'm running the code on - 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.
var wb = new XSSFWorkbook();
var createHelper = wb.getCreationHelper();
// Begin filling in rows/cells
addMostRecentSheet(wb);
var filepath = [hidden]
var fileOut = new java.io.FileOutputStream(filepath);
wb.write(fileOut);
fileOut.close();
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 os = enc.getDataStream(fs);
opc.saveImpl(os); //<<-----Crash there - unable to save /x1/styles.xml
opc.close();
var fos = new java.io.FileOutputStream(filepath);
fs.writeFilesystem(fos);
fos.close();
我最终设法保存了受密码保护的 xlsx,但我不得不删除日期列的样式.
I eventually managed to save password protected xlsx but I had to remove the styling of Date columns.
以下是将单元格格式化为日期单元格类型的代码:
Below is the code to format cells into Date celltype:
function createDateCell(row, colNum, value)
{
var cell;
if (value)
{
cell = row.createCell(colNum);
cell.setCellValue(value);
var cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd/mm/yyyy"));
cell.setCellStyle(cellStyle)
}
else
{
cell = row.createCell(colNum, Cell.CELL_TYPE_BLANK);
}
return cell;
}
但是在运行程序时,我不断收到此错误,是否有保留日期列类型的解决方法?错误信息:
Yet when running the program, I keep getting this error, is there a workaround to keep the Date column type? The error message:
org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException:
Fail to save: an error occurs while saving the package :
The part /xl/styles.xml failed to be saved in the stream with marshaller
org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller@216fb8e
推荐答案
要创建日期样式单元格,您需要这样做:
To create date style cells, you need to do this:
var wb = new XSSFWorkbook();
var createHelper = wb.getCreationHelper();
var dateStyle = wb.createCellStyle();
dateStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd/mm/yyyy"));
您只需要做一次.通过在顶部设置 cellStyle,您只填充了/xl/styles.xml 一次.
You only need to do it once. By setting the cellStyle at the top, you've populated /xl/styles.xml only once.
在填充单元格时,您只需将 dateStyle 添加到函数中:
When populating the cells, you simply add the dateStyle to the function:
createDateCell(row, colNum++, tables.SHE_SOUTH.DOB.value, dateStyle);
函数 createDateCell 只需添加 cellStyle:
The function createDateCell simply add the cellStyle:
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;
}
这样/xl/styles.xml 就不会像以前那样臃肿了.这允许加密工作簿.很好用,归功于 Axel Richter
That way the /xl/styles.xml doesn't get bloated as before. Which allows the encryption of the workbook. Works a treat, with credit to Axel Richter
这篇关于style.xml 破解受密码保护的 XSSFWorkbook (Apache POI v3.16) 保存过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!