我有一个下拉列表,其中包含一些选项,例如两个单元格。我需要的是关于所选选项,将其中一个单元格变为可编辑,将另一个单元格变为只读,反之亦然。
FileOutputStream fos;
try {
fos = new FileOutputStream("D:\\POIXls.xls");
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("new Sheet");
DataValidationHelper dvHelper = sheet.getDataValidationHelper();
DataValidationConstraint dvConstraint =
dvHelper.createExplicitListConstraint(new String[] { "cell 1 edit","cell 2 edit"});
CellRangeAddressList addressList = new CellRangeAddressList(0, 2, 0, 0);
DataValidation validation = dvHelper.createValidation(dvConstraint, addressList);
if (validation instanceof XSSFDataValidation) {
validation.setSuppressDropDownArrow(true);
validation.setShowErrorBox(true);
} else {
validation.setSuppressDropDownArrow(false);
}
sheet.addValidationData(validation);
workbook.write(fos);
fos.flush();
fos.close();
}catch(Exception e){//catch code}
我需要知道如何根据用户的选择使该 xls 文件使这些单元格可编辑/只读。 VB 代码也可能有帮助。
最佳答案
获取所需的单元格
并设置单元格样式
CellStyle unlockedCellStyle = wb.createCellStyle();
unlockedCellStyle.setLocked(true); //true or false based on the cell.
cell.setCellStyle(unlockedCellStyle);
希望对您有所帮助。
关于java - 如何使用 apache-POI 在 excel 中将单元格设置为只读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13250095/