制作excel表格只读

Gau*_*rav 0 java excel apache-poi

在使用Apache POI HSSF创建它之后,我想要只读excel表.我怎样才能做到这一点?

Tho*_*ber 5

可在此处找到详细说明:http: //systeminetwork.com/article/locking-cells-hssf

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

编辑

嗨Gaurav,这是完整和有效的代码:

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();