账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    java POI 实现excel表格下拉框选择一个值,另一个单元格自动填充内容
    85
    0

    假设有物品编号P1,P2,及其价格10,20,
    生成的excel表格第一行第一列是下拉列表,其中值为P1或P2,
    选择P1,第一行第二列自动填充价格10
    选择P2,第一行第二列自动填充价格20
    用java代码实现。
    有没有哪个大佬会的啊,百度巨久,就只能实现下拉,但是自动填充那真的是完全找不到。

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • じòぴé、殤 普通会员 1楼

      在Java POI库中,你可以使用AutoFillRowAutoFillColumn方法来实现Excel表格下拉框选择一个值,另一个单元格自动填充内容。以下是一个简单的示例:

      ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook;

      import java.io.FileOutputStream; import java.io.IOException; import java.util.List;

      public class ExcelAutoFillExample { public static void main(String[] args) { try { // 创建一个XSSFWorkbook对象 Workbook workbook = new XSSFWorkbook();

              // 创建一个工作表
              Sheet sheet = workbook.createSheet("AutoFill Example");
      
              // 创建一个行数和列数分别为2的行数和列数的表格
              Row row1 = sheet.createRow(0);
              Row row2 = sheet.createRow(1);
              Row row3 = sheet.createRow(2);
              Row row4 = sheet.createRow(3);
              Row row5 = sheet.createRow(4);
      
              // 添加下拉框和文本框
              Cell cell1 = row1.createCell(0);
              cell1.setCellValue("A1");
              cell2 = row1.createCell(1);
              cell2.setCellValue("B1");
              cell3 = row2.createCell(0);
              cell3.setCellValue("A2");
              cell4 = row2.createCell(1);
              cell4.setCellValue("B2");
              cell5 = row3.createCell(0);
              cell5.setCellValue("A3");
              cell6 = row3.createCell(1);
              cell6.setCellValue("B3");
      
              // 设置下拉框的值
              Cell dropdownCell1 = row1.createCell(2);
              dropdownCell1.setCellValue("C1");
              dropdownCell2 = row1.createCell(3);
              dropdownCell2.setCellValue("C2");
              dropdownCell3 = row2.createCell(2);
              dropdownCell3.setCellValue("C3");
      
              // 设置文本框的值
              Cell textCell1 = row1.createCell(4);
              textCell1.setCellValue("D1");
              textCell2 = row1.createCell(5);
              textCell2.setCellValue("D2");
              textCell3 = row2.createCell(5);
              textCell3.setCellValue("D3");
      
              // 设置下拉框和文本框的下拉值
              Cell dropdownCell1Formula = row1.createCell(6);
              dropdownCell1Formula.setCellValue("=C1");
              dropdownCell2Formula = row1.createCell(7);
              dropdownCell2Formula.setCellValue("=C2");
              dropdownCell3Formula = row2.createCell(8);
              dropdownCell3Formula.setCellValue("=C3");
      
              // 设置文本框的下拉值
              Cell textCell1Formula = row1.createCell(9);
              textCell1Formula.setCellValue("=D1");
              textCell2Formula = row1.createCell(10);
              textCell2Formula.setCellValue("=D2");
              textCell3Formula = row2.createCell(11);
              textCell3Formula.setCellValue("=D3");
      
              // 调用AutoFillRow方法,根据下拉框和文本框的选择填充值
              AutoFillRow fillRow = new AutoFillRow();
              fillRow.setFillType(FillType.AUTO);
              fillRow.setRange(row1, row2);
              fillRow.setFillValues(new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
      
    • 峩继续堕落 普通会员 3楼

      在Java POI库中,你可以使用AutoFillRowAutoFillColumn方法来实现Excel表格下拉框选择一个值,另一个单元格自动填充内容。以下是一个简单的示例:

      ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook;

      import java.io.FileOutputStream; import java.io.IOException; import java.util.List;

      public class ExcelAutoFillExample { public static void main(String[] args) { try { // 创建一个XSSFWorkbook对象 Workbook workbook = new XSSFWorkbook();

              // 创建一个工作表
              Sheet sheet = workbook.createSheet("AutoFill Example");
      
              // 创建一个行数和列数分别为2的行数和列数的表格
              Row row1 = sheet.createRow(0);
              Row row2 = sheet.createRow(1);
              Row row3 = sheet.createRow(2);
              Row row4 = sheet.createRow(3);
              Row row5 = sheet.createRow(4);
      
              // 添加下拉框和文本框
              Cell cell1 = row1.createCell(0);
              cell1.setCellValue("A1");
              cell2 = row1.createCell(1);
              cell2.setCellValue("B1");
              cell3 = row2.createCell(0);
              cell3.setCellValue("A2");
              cell4 = row2.createCell(1);
              cell4.setCellValue("B2");
              cell5 = row3.createCell(0);
              cell5.setCellValue("A3");
              cell6 = row3.createCell(1);
              cell6.setCellValue("B3");
      
              // 设置下拉框的值
              Cell dropdownCell1 = row1.createCell(2);
              dropdownCell1.setCellValue("C1");
              dropdownCell2 = row1.createCell(3);
              dropdownCell2.setCellValue("C2");
              dropdownCell3 = row2.createCell(2);
              dropdownCell3.setCellValue("C3");
      
              // 设置文本框的值
              Cell textCell1 = row1.createCell(4);
              textCell1.setCellValue("D1");
              textCell2 = row1.createCell(5);
              textCell2.setCellValue("D2");
              textCell3 = row2.createCell(5);
              textCell3.setCellValue("D3");
      
              // 设置下拉框和文本框的下拉值
              Cell dropdownCell1Formula = row1.createCell(6);
              dropdownCell1Formula.setCellValue("=C1");
              dropdownCell2Formula = row1.createCell(7);
              dropdownCell2Formula.setCellValue("=C2");
              dropdownCell3Formula = row2.createCell(8);
              dropdownCell3Formula.setCellValue("=C3");
      
              // 设置文本框的下拉值
              Cell textCell1Formula = row1.createCell(9);
              textCell1Formula.setCellValue("=D1");
              textCell2Formula = row1.createCell(10);
              textCell2Formula.setCellValue("=D2");
              textCell3Formula = row2.createCell(11);
              textCell3Formula.setCellValue("=D3");
      
              // 调用AutoFillRow方法,根据下拉框和文本框的选择填充值
              AutoFillRow fillRow = new AutoFillRow();
              fillR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