有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    如果在apache poi分析工作表时A列中的标志已经存在,那么CellStyle.setLocked可以设置为False,如果A列中有True。因此,如果工作表受到保护,这些单元格将不会被锁定并可编辑

    但是,如果A列中的TrueFalse将在ExcelGUI中更改,则这是不可能的,因为在更改A列中的值时,它不会有条件更改。为此,必须有条件格式化单元格锁定的可能性。但这种可能性并不存在,至少在不使用VBA的情况下是不存在的

    然后,只有数据验证是可能的,当B获取值时,它会检查A是否为真,如果不是,则发出警报Data Validations可以使用apache poi设置

    例如:

    import java.io.FileOutputStream;
    
    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.apache.poi.ss.util.CellRangeAddressList;
    
    public class CreateExcelDataValidationIfATrueThenB {
    
     public static void main(String[] args) throws Exception {
    
      Workbook workbook = new XSSFWorkbook();
      Sheet sheet = workbook.createSheet("Sheet1");
    
      DataValidationHelper dvHelper = sheet.getDataValidationHelper();
      DataValidationConstraint dvConstraint = dvHelper.createCustomConstraint("AND(A1, B1<>\"\")");
    
      CellRangeAddressList addressList = new CellRangeAddressList(-1, -1, 1, 1);
      DataValidation validation = dvHelper.createValidation(dvConstraint, addressList);
    
      validation.createPromptBox("For column B:", "If column A is True, then please put content in column B, else not.");
      validation.setShowPromptBox(true);
      validation.createErrorBox("Bad Value", "Please put content in column B only if column A is True, else not!");
      validation.setShowErrorBox(true);
    
      sheet.addValidationData(validation);
    
      sheet.createRow(0).createCell(0).setCellValue(true);
      sheet.createRow(1).createCell(0).setCellValue(false);
      sheet.createRow(2).createCell(0).setCellValue(false);
      sheet.createRow(3).createCell(0).setCellValue(true);
    
      workbook.write(new FileOutputStream("CreateExcelDataValidationIfATrueThenB.xlsx"));
      workbook.close();
    
     }
    
    }
    

    如上所述,在更改列A中的单元格时,可以使用VBA事件对单元格锁定进行条件格式化。但是apache poi不提供创建VBA代码。因此,如果这是必需的,那么必须使用Excel模板,该模板已经包含了这些宏