制作Excel工作表只读 [英] Making excel sheet read only

查看:247
本文介绍了制作Excel工作表只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想只读使用Apache POI HSSF创建后的Excel工作表。我该怎么做?

I want read only excel sheet after creating it using Apache POI HSSF. How can I do that?

推荐答案

一个详细的描述可以在这里找到:
http://systeminetwork.com/article/locking-cells-hssf

A detailed description can be found here: http://systeminetwork.com/article/locking-cells-hssf

基本上,你必须分配你的细胞自定义的 CellStyle CellStyle.setLocked(真)

Basically you have to assign your cells a custom CellStyle with CellStyle.setLocked(true)

喜拉夫,
下面是完整的和工作code:

Hi Gaurav, here is the complete and working code:


HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("sheet1");
/* password required for locks to become effective */
sheet.protectSheet("secretPassword");

/* cell style for locking */
CellStyle lockedCellStyle = workbook.createCellStyle();
lockedCellStyle.setLocked(true);
/* cell style for editable cells */
CellStyle unlockedCellStyle = workbook.createCellStyle();
unlockedCellStyle.setLocked(false);

/* cell which will be locked */
Cell lockedCell = sheet.createRow(0).createCell(0);
lockedCell.setCellValue("Hi, I'm locked...");
lockedCell.setCellStyle(lockedCellStyle);

/* unlocked cell */
Cell unlockedCell = sheet.createRow(1).createCell(0);
unlockedCell.setCellValue("Just edit me...");
unlockedCell.setCellStyle(unlockedCellStyle);

OutputStream out = new FileOutputStream("sample.xls");
workbook.write(out);
out.flush();
out.close();

这篇关于制作Excel工作表只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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