Excel 三级联动

Excel 三级联动选择一级清空二三级,选择二级情况三级

Private Sub Worksheet_Change(ByVal Target As Range)
 
Dim Rng As Range

'MsgBox (Target.Row)

If Target.Row < 2 Then Exit Sub
For Each Rng In Target

'MsgBox (Rng.Column)

If Rng.Column = 5 Then
Target.Next.ClearContents

Target.Next.Next.ClearContents
End If
If Rng.Column = 6 Then
Target.Next.ClearContents
End If
Next
End Sub

Excel 三级 联动 下拉框 宏代码 实例 代码注释 先在第一个下拉框加入一个valiation, 内容是 =$A$2:$A$5 Private Sub Worksheet_Change(ByVal Target As Range) ' Call back function which defined within according worksheet Dim i As Integer Dim tempStr As String Dim firstDrawBoxRowCount As Integer Dim firstDrawBoxColumn As Integer firstDrawBoxRowCount = 4 'Define the row number of first draw box firstDrawBoxColumn = 1 'Define the column number of ifrst draw box Dim secondDrawBoxRowCount As Integer Dim secondDrawBoxColumn As Integer secondDrawBoxRowCount = 33 'Define the row number of second draw box secondDrawBoxColumn = 4 'Define the column number of second draw box If Target.Column = 1 Then 'This defines the first column of draw box list, you can also define the row number of draw box list Cells(Target.Row, Target.Column + 1) = "" ' Do the clean first Cells(Target.Row, Target.Column + 1).Validation.Delete Cells(Target.Row, Target.Column + 2) = "" Cells(Target.Row, Target.Column + 2).Validation.Delete For i = 2 To firstDrawBoxRowCount + 1 'Enter the cycle to find out the content for column 2 If Trim(Cells(Target.Row, Target.Column)) = Trim(Cells(i, firstDrawBoxColumn)) Then tempStr = Trim(Cells(i, firstDrawBoxColumn + 1)) 'Find out the options for second draw box, it is seperated by , Cells(Target.Row, Target.Column + 1).Select ' Fill the validation to second draw box With Selection.Validation .Delete .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ xlBetween, Formula1:=tempStr .IgnoreBlank = True .InCellDropdown = True .InputTitle = "" .ErrorTitle = "" .InputMessage = "" .ErrorMessage = "" .IMEMode = xlIMEModeNoControl .ShowInput = True .ShowError = True
可以通过使用Apache POI库来实现Java设置Excel三级联动,其中二级不选择时,只有一级和三级联动。 下面是一个简单的示例代码,实现了基于POI的Excel三级联动: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; public class ExcelHelper { public static void main(String[] args) throws IOException { String inputFile = "input.xlsx"; String outputFile = "output.xlsx"; // 读取Excel文件 FileInputStream inputStream = new FileInputStream(inputFile); Workbook workbook = WorkbookFactory.create(inputStream); Sheet sheet = workbook.getSheetAt(0); // 获取一级选项列表 List<String> level1Options = getLevel1Options(sheet); // 获取三级选项列表 List<String> level3Options = getLevel3Options(sheet); // 添加一级选项列和三级选项列 addLevel1Column(sheet, level1Options); addLevel3Column(sheet, level3Options); // 写入Excel文件 FileOutputStream outputStream = new FileOutputStream(outputFile); workbook.write(outputStream); workbook.close(); outputStream.close(); System.out.println("Excel文件处理完成!"); } /** * 获取一级选项列表 */ private static List<String> getLevel1Options(Sheet sheet) { List<String> level1Options = new ArrayList<>(); int lastRowNum = sheet.getLastRowNum(); for (int i = 1; i <= lastRowNum; i++) { Row row = sheet.getRow(i); if (row != null) { Cell cell = row.getCell(0); if (cell != null && cell.getCellType() == CellType.STRING) { String option = cell.getStringCellValue(); if (!level1Options.contains(option)) { level1Options.add(option); } } } } return level1Options; } /** * 获取三级选项列表 */ private static List<String> getLevel3Options(Sheet sheet) { List<String> level3Options = new ArrayList<>(); int lastRowNum = sheet.getLastRowNum(); for (int i = 1; i <= lastRowNum; i++) { Row row = sheet.getRow(i); if (row != null) { Cell cell = row.getCell(2); if (cell != null && cell.getCellType() == CellType.STRING) { String option = cell.getStringCellValue(); if (!level3Options.contains(option)) { level3Options.add(option); } } } } return level3Options; } /** * 添加一级选项列 */ private static void addLevel1Column(Sheet sheet, List<String> level1Options) { int lastRowNum = sheet.getLastRowNum(); for (int i = 1; i <= lastRowNum; i++) { Row row = sheet.getRow(i); if (row != null) { Cell cell = row.getCell(0); if (cell != null && cell.getCellType() == CellType.STRING) { String option = cell.getStringCellValue(); int level1Index = level1Options.indexOf(option); Cell level1Cell = row.createCell(3); level1Cell.setCellValue(level1Index + 1); } } } // 添加一级选项标题 Row titleRow = sheet.getRow(0); Cell level1TitleCell = titleRow.createCell(3); level1TitleCell.setCellValue("一级选项"); } /** * 添加三级选项列 */ private static void addLevel3Column(Sheet sheet, List<String> level3Options) { int lastRowNum = sheet.getLastRowNum(); for (int i = 1; i <= lastRowNum; i++) { Row row = sheet.getRow(i); if (row != null) { Cell cell = row.getCell(2); if (cell != null && cell.getCellType() == CellType.STRING) { String option = cell.getStringCellValue(); int level3Index = level3Options.indexOf(option); Cell level3Cell = row.createCell(4); level3Cell.setCellValue(level3Index + 1); } } } // 添加三级选项标题 Row titleRow = sheet.getRow(0); Cell level3TitleCell = titleRow.createCell(4); level3TitleCell.setCellValue("三级选项"); } } ``` 在上面的代码中,我们首先读取Excel文件,然后获取一级选项列表和三级选项列表。接着,我们在Excel表格中添加一个名为“一级选项”的列和一个名为“三级选项”的列。对于每一行数据,我们将其一级选项的值转换为对应的序号,并将其写入新添加的一级选项列中。同样,对于每一行数据,我们将其三级选项的值转换为对应的序号,并将其写入新添加的三级选项列中。最后,我们将处理后的Excel文件写入磁盘。 需要注意的是,上面的代码仅仅是一个示例,实际应用中可能需要根据具体的需求进行修改。同时,上面的代码也没有考虑到错误处理和异常情况,实际应用中需要注意这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值