Ahm*_*tti 3 java excel vba apache-poi
我有一个下拉列表包含一些选项,例如两个单元格。我需要的是关于所选选项,将其中一个单元格设置为可编辑,将另一个单元格设置为只读,反之亦然。
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}
Run Code Online (Sandbox Code Playgroud)
我需要知道如何使 xls 文件根据用户的选择使这些单元格可编辑/只读。VB 代码也可能有帮助。
获取cell您想要的并设置您的单元格样式
CellStyle unlockedCellStyle = wb.createCellStyle();
unlockedCellStyle.setLocked(true); //true or false based on the cell.
cell.setCellStyle(unlockedCellStyle);
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
16465 次 |
| 最近记录: |