styles.xml破坏受密码保护的XSSFWorkbook(Apache POI v3.16)保存过程[已解决] [英] styles.xml breaking password-protected XSSFWorkbook (Apache POI v3.16) save process [SOLVED]

查看:117
本文介绍了styles.xml破坏受密码保护的XSSFWorkbook(Apache POI v3.16)保存过程[已解决]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前在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(

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?)

注意:我正在使用Mozilla Rhino v1.7R3在服务器端Javascript上运行代码,它几乎支持所有ECMAScript Edition 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,但是我不得不删除了Date列的样式.

I eventually managed to save password protected xlsx but I had to remove the styling of Date columns.

下面是将单元格格式化为Date单元格类型的代码:

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;
}

但是,在运行程序时,我一直收到此错误,是否有解决方法来保持Date列类型? 错误消息:

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

这篇关于styles.xml破坏受密码保护的XSSFWorkbook(Apache POI v3.16)保存过程[已解决]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆