Apache的POI - 爪哇 - 迭代在Excel列 [英] Apache POI - JAVA - iterating over columns in excel

查看:198
本文介绍了Apache的POI - 爪哇 - 迭代在Excel列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对Java在这里。我工作的一个code读取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

我写了一些值(使用XLWT)2栏看起来像这样的:

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的code的任何文档。我相信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细胞的迭代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 /s$p$padsheet/quick-guide.html#Iterator

但我只能找到code处理行和单元格,而不是列。我想通过列,虽然进行迭代。是否有任何code来处理这个?

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 - 爪哇 - 迭代在Excel列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆