锁定使用Apache POI在Excel中单列 [英] Lock single column in Excel using Apache POI

查看:604
本文介绍了锁定使用Apache POI在Excel中单列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Excel中,其中只有一个特定的列已被锁定(只读),其余都是可编辑的,

I want to create a Excel in which only a specific column is locked(Read-only), and the rest are editable,

我用下面的方法,但似乎并没有工作。

I am using the following approach, but that doesn't seem to work.

创建两个CellStyles,其中一个setLocked(真)和其他带setLocked(假)。

Create two CellStyles, one with setLocked(true) and other with setLocked(false).

然后应用锁定样式对于其中需要锁定柱,和非锁定式的所有其他细胞的所有细胞

Then apply the locked style for all the cells in the column which needs to be locked, and the unlocked style for all the other cells.

使用sheet.protectSheet()保护工作表;

Protect the sheet using sheet.protectSheet("");

但是,当我在开放式办公室打开创建的Excel,我注意到,所有的细胞都被锁定!

But when I open the created Excel in open office, I notice that all the cells are locked!

他们都不是编辑。

我怎样才能实现上述要求?

How can I achieve the above requirement?

P.S:我不能使用数据验证方法

P.S : I cant use the data validation approach.

推荐答案

如果你做它的工作原理相反。保护整个表,并调用 setLocked(假)为这应该是可编辑的单元格。

If you do the opposite it works. Protect the whole sheet and call setLocked(false) for the cells which should be editable.

String file = "c:\\poitest.xlsx";
FileOutputStream outputStream = new FileOutputStream(file);
Workbook wb = new XSSFWorkbook();

CellStyle unlockedCellStyle = wb.createCellStyle();
unlockedCellStyle.setLocked(false);

Sheet sheet = wb.createSheet();
sheet.protectSheet("password");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("TEST");
cell.setCellStyle(unlockedCellStyle);

wb.write(outputStream);
outputStream.close();

这篇关于锁定使用Apache POI在Excel中单列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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