java 使excel表只读

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3746441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:14:37  来源:igfitidea点击:

Making excel sheet read only

javaexcelapache-poi

提问by Gaurav

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

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

回答by Thomas Weber

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

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

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

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

Edited

已编辑

Hi Gaurav, here is the complete and working code:

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

回答by JoseK

Here is some testedcode that works in making the specific cell readonly. Based on your comment in @Thomas Weber's answer.

这是一些经过测试的代码,可用于使特定单元格只读。根据您在@Thomas Weber 的回答中的评论。

This sets an initial value in a cell, then it uses a data constraint to ensure that fixed value cannot be modified by the user in Excel. Try it out.

这会在单元格中设置一个初始值,然后它使用数据约束来确保用户无法在 Excel 中修改固定值。试试看。

HSSFWorkbook      workBook = new HSSFWorkbook ();
HSSFSheet         sheet1    = workBook.createSheet();

HSSFRow row1 = sheet1.createRow(10); 
HSSFCell cell1 = row1.createCell(0);
cell1.setCellValue("text: The new line which should be locked"); // SETTING INITIAL VALUE

HSSFCell displayNameCell = cell1;

String[] displayNameList = new String[]{"text: The new line which should be locked"}; //ADDING SAME VALUE INTO A STRING ARRAY AS THE RESTRICTED VALUE 

DVConstraint displayNameConstraint = DVConstraint.createExplicitListConstraint(displayNameList);

CellRangeAddressList displayNameCellRange = new CellRangeAddressList(displayNameCell.getRowIndex(),displayNameCell.getRowIndex(),displayNameCell.getColumnIndex(),displayNameCell.getColumnIndex());

HSSFDataValidation displayNameValidation = new HSSFDataValidation(displayNameCellRange,displayNameConstraint);

displayNameValidation.createErrorBox("Not Applicable","Cannot change the value");

displayNameValidation.setSuppressDropDownArrow(true);
displayNameCell.getSheet().addValidationData(displayNameValidation);





  // Write the output to a file
     FileOutputStream fileOut1 = new FileOutputStream("D:\book.xls");
     workBook.write(fileOut1);
     fileOut1.close();      

This code is based on this thread http://osdir.com/ml/user-poi.apache.org/2009-07/msg00056.html

此代码基于此线程http://osdir.com/ml/user-poi.apache.org/2009-07/msg00056.html

回答by Bozho

new File("/path/to/file.xls").setReadOnly();