You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java如何使用POI读取xls指定列值并填充到JComboBox中

实现步骤

通过「Apache POI解析xls文件 + 绑定Swing组件数据模型」即可实现功能,具体流程如下:

  • 引入Apache POI的HSSF模块依赖,专门用于解析.xls格式(Excel 97-2003版本)的文件
  • 加载目标xls文件,定位到名为clients的工作表
  • 读取表头行确认Full name列的索引(按你给出的列顺序,从0开始计数该列索引为2,动态匹配表头可以避免列顺序变动导致的取值错误)
  • 遍历所有有效数据行,提取Full name列的非空值存入集合
  • 将集合中的值绑定到JComboBox的数据模型,注意所有Swing组件操作需在事件调度线程(EDT)中执行,避免界面渲染异常
完整代码示例
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import javax.swing.*;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

public class ClientComboBoxLoader {
    private static JFrame mainFrame;
    private static JComboBox<String> clientNameComboBox;

    public static void main(String[] args) {
        // Swing组件初始化必须放在事件调度线程中
        SwingUtilities.invokeLater(() -> {
            mainFrame = new JFrame("客户选择测试");
            mainFrame.setSize(420, 160);
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            clientNameComboBox = new JComboBox<>();
            mainFrame.add(clientNameComboBox);
            
            // 替换为你的xls文件实际路径
            loadNameDataToCombo("D:/data/clients.xls");
            
            mainFrame.setVisible(true);
        });
    }

    private static void loadNameDataToCombo(String excelFilePath) {
        List<String> fullNameList = new ArrayList<>();
        // 用try-with-resources自动关闭流和工作簿资源
        try (FileInputStream fis = new FileInputStream(excelFilePath);
             Workbook workbook = new HSSFWorkbook(fis)) {

            // 定位目标工作表
            Sheet clientSheet = workbook.getSheet("clients");
            if (clientSheet == null) {
                JOptionPane.showMessageDialog(mainFrame, "未找到名为clients的工作表");
                return;
            }

            // 匹配Full name列的索引
            Row headerRow = clientSheet.getRow(0);
            int fullNameColIdx = -1;
            for (int i = 0; i < headerRow.getLastCellNum(); i++) {
                Cell headerCell = headerRow.getCell(i);
                if (headerCell != null && "Full name".equals(headerCell.getStringCellValue().trim())) {
                    fullNameColIdx = i;
                    break;
                }
            }
            if (fullNameColIdx == -1) {
                JOptionPane.showMessageDialog(mainFrame, "表中不存在Full name列");
                return;
            }

            // 遍历数据行(第1行是表头,从第2行开始读数据)
            for (int rowNum = 1; rowNum <= clientSheet.getLastRowNum(); rowNum++) {
                Row dataRow = clientSheet.getRow(rowNum);
                if (dataRow == null) continue; // 跳过空行
                Cell nameCell = dataRow.getCell(fullNameColIdx);
                if (nameCell != null && nameCell.getCellType() == CellType.STRING) {
                    String fullName = nameCell.getStringCellValue().trim();
                    if (!fullName.isEmpty()) {
                        fullNameList.add(fullName);
                    }
                }
            }

            // 给下拉框绑定数据
            DefaultComboBoxModel<String> comboModel = new DefaultComboBoxModel<>();
            fullNameList.forEach(comboModel::addElement);
            clientNameComboBox.setModel(comboModel);

        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(mainFrame, "读取Excel失败:" + e.getMessage());
        }
    }
}
注意事项
  • 如果用Maven管理项目,只需在pom.xml中引入poi依赖即可,不需要额外引入处理.xlsx格式的poi-ooxml包,选择常用稳定版即可。
  • 代码中已经做空行、空单元格、目标资源不存在的异常兜底,避免运行时抛出空指针。
  • 如果数据量很大,建议将Excel读取逻辑放在子线程中执行,读取完成后再通过SwingUtilities.invokeLater更新下拉框模型,避免阻塞界面。

内容的提问来源于stack exchange,提问作者Alessandro Provenzano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 22:54:29