您的位置:首页 > 其它

Poi操作保护工作表后指定区域内删除行列

2015-11-28 16:38 621 查看
最近做的一个物流项目,其主要操作是处理上传和下载Excel文件的,主要用的第三方的Poi的jar包。前几天遇到个问题,纠结了好几天终于得以解决,现将过程记录下。

作成结构相同的多sheet的Excel文件,其中根据业务要求,sheet中分为两块,一块是固定部分要求锁定,

不能做修改、插入及删除行列的操作,另一部分是动态部分,只允许删除列。

本来以为不难实现,然而在做到动态部分允许删除列时,一直存在问题,开始的思路是在模板文件的sheet中将动态可删除列部分全选中,右击设置单元格格式–在保护界面将锁定前面的勾去掉,再设置保护sheet(审阅–保护工作表–勾选上面的保护工作表及锁定的单元格内容–勾选删除列及其他需要的属性),然后复制模板sheet将数据写入就可以了。

然而在做sheet复制后,新复制出来的sheet丢失了保护属性,看来在模板中设置保护属性再复制的思路行不通,郁闷,于是上网搜索,提到poi中对于sheet对象提供了protectSheet(String password)方法,于是对于新复制出来的sheet设置该属性,可是问题又来了,下载下来的Excel文件保护工作表选项中没有勾选删除列,于是查看Poi源码,发现protectSheet的方法如下:

@Override
public void protectSheet(String password) {
if (password != null) {
CTSheetProtection sheetProtection = safeGetProtectionField();
setSheetPassword(password, null); // defaults to xor password
sheetProtection.setSheet(true);
sheetProtection.setScenarios(true);
sheetProtection.setObjects(true);
} else {
worksheet.unsetSheetProtection();
}
}


继续查看CTSheetProtection类有没有相关的属性设置,还好,有deleteColumns之类的属性,那么就好办了,于是重写Poi的protectSheet方法如下:

public void protectSheet(XSSFSheet workSheet, String password) {
if (password != null) {
CTSheetProtection sheetProtection = safeGetProtectionField(workSheet);
workSheet.setSheetPassword(password, null);
sheetProtection.setSheet(true);
sheetProtection.setScenarios(true);
sheetProtection.setObjects(true);
sheetProtection.setSelectLockedCells(true);
sheetProtection.setSelectUnlockedCells(true);
sheetProtection.setFormatCells(true);
sheetProtection.setFormatColumns(true);
sheetProtection.setFormatRows(true);
sheetProtection.setDeleteColumns(true);
sheetProtection.setSort(true);
sheetProtection.setAutoFilter(true);
} else {
workSheet.getCTWorksheet().unsetSheetProtection();
}
}
private CTSheetProtection safeGetProtectionField(XSSFSheet workSheet) {
if (!workSheet.getCTWorksheet().isSetSheetProtection()) {
return workSheet.getCTWorksheet().addNewSheetProtection();
}
return workSheet.getCTWorksheet().getSheetProtection();
}


写完后下载成功,打开Excel文件,右击列,擦,删除列还是灰色的,怎么回事? 无奈去查poi文档,

发现是属性设置反了,于是修改如下:

CTSheetProtection sheetProtection = safeGetProtectionField(workSheet);
workSheet.setSheetPassword(password, null);
sheetProtection.setSheet(true);
sheetProtection.setScenarios(true);
sheetProtection.setObjects(true);
sheetProtection.setSelectLockedCells(false);
sheetProtection.setSelectUnlockedCells(false);
sheetProtection.setFormatCells(false);
sheetProtection.setFormatColumns(false);
sheetProtection.setFormatRows(false);
sheetProtection.setDeleteColumns(false);
sheetProtection.setSort(false);
sheetProtection.setAutoFilter(false);


重新下载打开Excel文件,右击列,删除列亮了,点击删除,擦,还让不让人活了!!!

“你正试图删除包含有锁定单元格的一列。工作表受保护时无法删除锁定单元格。”怎么会有锁定单元格呢? 哎,继续调查,发现工程中的共通方法复制sheet代码部分如下:

// create new sheet
Sheet newSheet = workbook.createSheet(newSheetName);

// copy from template sheet
PoiUtil.copySheet(workbook, templateSheet, newSheet);


终于知道问题了,Excel在新建sheet时,默认是所有单元格都带锁定的,而后面的copySheet只是将模板中有数据部分的数据和式样复制到新sheet,但是新sheet中其余部分的空白单元格都是带锁的,故而发生以上问题,于是将上段修改为clonesheet方法:

// create new sheet
Sheet newSheet = workbook.cloneSheet(workbook.getSheetIndex(templateSheetName));
workbook.setSheetName(workbook.getSheetIndex(newSheet), newSheetName);

// copy from template sheet
PoiUtil.copySheet(workbook, templateSheet, newSheet);


下载成功,右击删除列成功,虽然折腾很久,但是最终搞出来了,还是很开心的,只是clonesheet方法时,当源sheet中有公式时,打开报Excel发生损坏,我的机能中没有公式就没有继续调查了,如果有朋友知道的话,欢迎回复,谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息