Apache POI - JAVA - 在 excel 中迭代列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34664899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:10:43  来源:igfitidea点击:

Apache POI - JAVA - iterating over columns in excel

javaexceliterationapache-poicell

提问by Kat

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:

这里是java的新手。我正在编写一个读取 excel 文件的代码(查看列中的单元格),然后编写类似于下表的内容:

I have an excel file that looks like this:

我有一个看起来像这样的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

I'm writing some values (using XLWT) in column 2 that look like this:

我正在第 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

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.

本质上,正在比较第 3 列和第 1 列,如果单元格 (1,1) 具有与第 3 列匹配的值,则将第 4 列的值(对应于第 3 列)写入第 2 列。

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.

我实际上已经在 Python 中完成了这项工作,并且我考虑过使用 Jython,但是,我找不到任何关于将 Python 模块导入 Jython 代码的文档。我相信 XSSF apache-poi 是我可以在 java 中处理 xlsx 文件的唯一方法。

Relevant page: How to do cell iteration of excel in java

相关页面:如何在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

Mainly looking for a way to iterate through column values. I looked through the documentation: http://poi.apache.org/spreadsheet/quick-guide.html#Iterator

主要是寻找一种遍历列值的方法。我查看了文档: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?

但我只能找到处理行和单元格的代码,而不是列。我想遍历列。有没有代码可以解决这个问题?

回答by dlopatin

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);
}