这里是 java 的新手。我正在编写读取 excel 文件的代码(查看列中的单元格),然后编写类似于下表的内容:
我有一个如下所示的 excel 文件:
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),如下所示:
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 列。
我实际上是用 Python 完成的,我考虑过使用 Jython,但是,我找不到任何关于将 python 模块导入 Jython 代码的文档。我相信 XSSF apache-poi 是我可以在 java 中处理 xlsx 文件的唯一方法。
相关页面:How to do cell iteration of excel in java
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
但我只能找到处理行和单元格的代码,而不是列。不过,我想遍历这些列。有什么代码可以解决这个问题吗?
最佳答案
只需遍历行并获取所需的单元格:
int columnIndex = 3;
for (int rowIndex = 0; rowIndex<3; rowIndex++){
Row row = CellUtil.getRow(rowIndex, sheet);
Cell cell = CellUtil.getCell(row, columnIndex);
}
关于java - Apache POI - JAVA - 遍历 excel 中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34664899/