Java-使用POI在Jcombobox中打印特定的列值
我有一个.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要使用Apache POI读取Excel文件,需要从Excel文件创建
Workbook
对象。然后选择所需的纸张,然后在所需的行上迭代。然后,从所需的列中获取单元格值。参见忙碌的开发人员HSSF和XSSF功能 apache poi。
获取单元格值的最简单方法是使用Apache POI的
dataformatter
。dataformatter.formatcellvalue
将单元格作为字符串的格式与Excel中所示。使用当前Apache POI 5.2.2
,dataformatter.setusecachedvaluesforformulacells
可以设置true
。然后,当单元格值为公式结果时,不需要公式评估。在行上迭代并获取单元格时,需要知道完全空的行存储在工作表的数据中。因此,
sheet.getrow
可能返回null
。也没有存储未使用的细胞。因此,row.getCell
也可能返回null
。这就是为什么需要进行无效检查的原因。如果空细胞无关紧要,则可以在row.getCell
中使用row.missingcellpolicy.return_blank_as_null
。然后,空单元还以null
返回。数据可以存储在数据结构中,然后可用于构建
Jcombobox
。向量
将提供,因为有一个构造函数 public jcombobox(向量项目)。完整的示例:
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 currentapache poi 5.2.2
,DataFormatter.setUseCachedValuesForFormulaCells
can be settrue
. 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 returnnull
. Also unused cells are not stored. SoRow.getCell
also might returnnull
. That's why null-checks are necessary. And if empty cells shall not matter,Row.MissingCellPolicy.RETURN_BLANK_AS_NULL
can be used inRow.getCell
. Then empty cells also get returned asnull
.The data could be stored in a data structure which then can be used to construct a
JComboBox
. AVector
would offer, as there is a constructor public JComboBox(Vector items).Complete example: