Java-使用POI在Jcombobox中打印特定的列值

发布于 2025-02-13 21:40:28 字数 125 浏览 0 评论 0原文

我有一个.xls文件,只有一个名为“客户端”的表格,它包含以下数据:

name |姓|全名|出生日期|城市

我需要在“全名”列中获取所有值,我需要在Jcombobox中打印它们。 有办法这样做吗? 先感谢您

I have a .xls file with only one sheet called "clients" and it contains data as it follows:

Name | Surname | Full name | Date of birth | City

I need to get all the values in the "Full name" column and I need to print them in a jComboBox.
Is there a way of doing that?
Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Saygoodbye 2025-02-20 21:40:28

要使用Apache POI读取Excel文件,需要从Excel文件创建Workbook对象。然后选择所需的纸张,然后在所需的行上迭代。然后,从所需的列中获取单元格值。

参见忙碌的开发人员HSSF和XSSF功能 apache poi。

获取单元格值的最简单方法是使用Apache POI的dataformatterdataformatter.formatcellvalue将单元格作为字符串的格式与Excel中所示。使用当前Apache POI 5.2.2dataformatter.setusecachedvaluesforformulacells可以设置true。然后,当单元格值为公式结果时,不需要公式评估。

在行上迭代并获取单元格时,需要知道完全空的行存储在工作表的数据中。因此,sheet.getrow可能返回null。也没有存储未使用的细胞。因此,row.getCell也可能返回null。这就是为什么需要进行无效检查的原因。如果空细胞无关紧要,则可以在row.getCell中使用row.missingcellpolicy.return_blank_as_null。然后,空单元还以null返回。

数据可以存储在数据结构中,然后可用于构建Jcombobox向量将提供,因为有一个构造函数 public jcombobox(向量项目)

完整的示例:

import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.*;
import java.util.Vector; 
import javax.swing.*;

public class ReadExcelColumnToVector {
    
 private static final DataFormatter dataFormatter = new DataFormatter(java.util.Locale.US);
 private Workbook workbook; 
 
 public ReadExcelColumnToVector(String filePath) throws Exception {
  this.workbook = WorkbookFactory.create(new FileInputStream(filePath));
  this.dataFormatter.setUseCachedValuesForFormulaCells(true);
 }
        
 public Vector<String> getData(int sheetIndex, int firstRow, int dataCol) {
  Sheet sheet = this.workbook.getSheetAt(sheetIndex);   
  Vector<String> data  = new Vector<String>();
  String cellValue = "";
  for (int r = firstRow; r <= sheet.getLastRowNum(); r++) {
   Row row = sheet.getRow(r);
   if (row != null) {
    Cell cell = row.getCell(dataCol, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
    if (cell != null) {
     cellValue = this.dataFormatter.formatCellValue(cell);
     data.add(cellValue);
    }
   }
  }
  return data;
 }
 
 public void closeWorkbook() {
  try {
   this.workbook.close();  
  } catch (Exception ex) {
   ex.printStackTrace();
  }      
 }
 
 public static void main(String[] args) throws Exception {
  
  //ReadExcelColumnToVector app = new ReadExcelColumnToVector("./ExcelExample.xlsx");   
  ReadExcelColumnToVector app = new ReadExcelColumnToVector("./ExcelExample.xls");   
  
  //get data from first sheet, starting on second row, third column
  Vector<String> data = app.getData(0, 1, 2); // all indexes ara 0-based
  
  app.closeWorkbook();
  
  System.out.println(data);
  // use Vector<String> data to construct a JComboBox
  JFrame frame = new JFrame("JComboBox Sample");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(500, 100);
  JPanel panel = new JPanel();
  JComboBox<String> comboBox = new JComboBox<String>(data);
  panel.add(comboBox);
  frame.add(panel);
  frame.setVisible(true);
 
 }
}

To read Excel files using Apache POI one needs creating a Workbook object from a Excel file. Then one chooses the needed sheet and iterates over the needed rows. Then one gets the cell values from the needed column.

See Busy Developers' Guide to HSSF and XSSF Features for how to use Apache POI.

The simplest way to get the cell values is using DataFormatter of Apache POI. DataFormatter.formatCellValue gets the cell values as string in the same format as shown in Excel. Using current apache poi 5.2.2, DataFormatter.setUseCachedValuesForFormulaCells can be set true. Then no formula evaluation is needed when cell values are formula results.

While iterating over the rows and getting cells one needs to know that totally empty rows are not stored in sheet's data. So Sheet.getRow might return null. Also unused cells are not stored. So Row.getCell also might return null. That's why null-checks are necessary. And if empty cells shall not matter, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL can be used in Row.getCell. Then empty cells also get returned as null.

The data could be stored in a data structure which then can be used to construct a JComboBox. A Vector would offer, as there is a constructor public JComboBox(Vector items).

Complete example:

import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.*;
import java.util.Vector; 
import javax.swing.*;

public class ReadExcelColumnToVector {
    
 private static final DataFormatter dataFormatter = new DataFormatter(java.util.Locale.US);
 private Workbook workbook; 
 
 public ReadExcelColumnToVector(String filePath) throws Exception {
  this.workbook = WorkbookFactory.create(new FileInputStream(filePath));
  this.dataFormatter.setUseCachedValuesForFormulaCells(true);
 }
        
 public Vector<String> getData(int sheetIndex, int firstRow, int dataCol) {
  Sheet sheet = this.workbook.getSheetAt(sheetIndex);   
  Vector<String> data  = new Vector<String>();
  String cellValue = "";
  for (int r = firstRow; r <= sheet.getLastRowNum(); r++) {
   Row row = sheet.getRow(r);
   if (row != null) {
    Cell cell = row.getCell(dataCol, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
    if (cell != null) {
     cellValue = this.dataFormatter.formatCellValue(cell);
     data.add(cellValue);
    }
   }
  }
  return data;
 }
 
 public void closeWorkbook() {
  try {
   this.workbook.close();  
  } catch (Exception ex) {
   ex.printStackTrace();
  }      
 }
 
 public static void main(String[] args) throws Exception {
  
  //ReadExcelColumnToVector app = new ReadExcelColumnToVector("./ExcelExample.xlsx");   
  ReadExcelColumnToVector app = new ReadExcelColumnToVector("./ExcelExample.xls");   
  
  //get data from first sheet, starting on second row, third column
  Vector<String> data = app.getData(0, 1, 2); // all indexes ara 0-based
  
  app.closeWorkbook();
  
  System.out.println(data);
  // use Vector<String> data to construct a JComboBox
  JFrame frame = new JFrame("JComboBox Sample");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(500, 100);
  JPanel panel = new JPanel();
  JComboBox<String> comboBox = new JComboBox<String>(data);
  panel.add(comboBox);
  frame.add(panel);
  frame.setVisible(true);
 
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文