Apache POI - JAVA - iterating over columns in excel(Apache POI - JAVA - 遍历 excel 中的列)
问题描述
这里是java新手.我正在编写一个读取 excel 文件的代码(查看列中的单元格),然后编写如下表所示的内容:
new to java here. I'm working on a code that reads excel files (looking at cells in columns) and then writes something that looks like the following tables:
我有一个如下所示的 excel 文件:
I have an excel file that looks like this:
col1 col2 col3 col4
-----------------------------
row1 | 2,3,1 _ 1 w
row2 | 3,2,7 _ 2 x
row3 | _ _ 3 y
row4 | 4,9 _ 4 z
我在第 2 列中写了一些值(使用 XLWT),如下所示:
I'm writing some values (using XLWT) in column 2 that look like this:
col1 col2 col3 col4
-----------------------------
row1 | 2,3,1 x,y,w 1 w
row2 | 3,2,7 y,x 2 x
row3 | _ _ 3 y
row4 | 4,9 z 4 z
本质上,第 3 列和第 1 列正在比较,如果单元格 (1,1) 的值与第 3 列匹配,则第 4 列的值(对应于第 3 列)写入第 2 列.
Essentially, column 3 and column 1 are being compared, and if cell (1,1) has values matched in column 3, column 4's values (which correspond to column 3) are written into column 2.
我实际上已经在 Python 中完成了这项工作,并且我考虑使用 Jython,但是,我找不到任何关于将 python 模块导入 Jython 代码的文档.我相信 XSSF apache-poi 是我可以在 java 中处理 xlsx 文件的唯一方法.
I've done this in Python actually, and I considered using Jython, however, I couldn't find any docs on importing python modules into Jython code. I believe XSSF apache-poi is the only way I can deal with xlsx files in java.
相关页面:如何在excel中进行单元格迭代爪哇
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
public class Python {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream(new File("C:\Users\...\Bioactives25s.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet worksheet = workbook.getSheetAt(0);
for (int i = 0; i < 9999; i++) {
XSSFRow row = worksheet.getRow(i);
if(row == null) {
//iterates over rows
}
for (int j = 0; j < 30; j++) {
XSSFCell cell = row.getCell(j);
if(cell == null) {
//iterates over cells
主要是寻找一种遍历列值的方法.我浏览了文档:http://poi.apache.org/spreadsheet/quick-guide.html#Iterator
Mainly looking for a way to iterate through column values. I looked through the documentation: http://poi.apache.org/spreadsheet/quick-guide.html#Iterator
但我只能找到处理行和单元格的代码,而不是列.不过,我想遍历这些列.有什么代码可以解决这个问题吗?
But I could only find code for dealing with rows and cells, not columns. I'd like to iterate through the columns though. Is there any code to deal with this?
推荐答案
只需遍历行并获取所需的单元格:
Just iterate through rows and take required cell:
int columnIndex = 3;
for (int rowIndex = 0; rowIndex<3; rowIndex++){
Row row = CellUtil.getRow(rowIndex, sheet);
Cell cell = CellUtil.getCell(row, columnIndex);
}
这篇关于Apache POI - JAVA - 遍历 excel 中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Apache POI - JAVA - 遍历 excel 中的列
基础教程推荐
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01