如何修改代码,以便仅打印单元格E和F? [英] How do I modify the code so it only prints cells E and F?

查看:116
本文介绍了如何修改代码,以便仅打印单元格E和F?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个关于java的小程序来阅读一个excel,但是如何修改它,只能打印某些单元格?

  public class Read 
{
public static void main(String [] args)throws异常
{
文件f =新建文件(/ users / Me / Documents / Test的.xls);
工作簿wb = Workbook.getWorkbook(f);
工作表s = wb.getSheet(0);
int row = s.getRows();
int col = s.getColumns(); (int i = 0; i< row; i ++)

$($ j


$ b Cell c = s.getCell(j,i);
System.out.print(c.getContents()+\t);
}
}

System.out.println();
}

}


解决方案

如果要打印每个E单元格,则需要删除列循环,但保留行循环,并将E的索引硬编码到getCell(x,y)函数中。我们知道'E'是字母表中的索引4,如果我们使用代码,

  public static void main(String [ ] args)throws Exception {
File f = new File(/users/Me/Documents/Test.xls);
工作簿wb = Workbook.getWorkbook(f);
工作表s = wb.getSheet(0);
int row = s.getRows();
int col = s.getColumns(); (int i = 0; i< row; i ++){
单元格c = s.getCell(4,i);

System.out.print(c.getContents()+\t);
}
System.out.println();
}

只会打印E列中的列。



对于您的后续问题,如何检查一个单元格的内容是否大于其他单元...您的主要问题是您要比较两个字符串,以便检查如果一个值大于另一个值,则必须将该字符串解析为int(或其他数据类型)。在java中,这可以通过转换或使用数据类型函数给我们的方法来完成。下面我们将比较细胞C1和E1的一个例子。

  Cell c = s.getCell(2,1); 
单元格e = s.getCell(4,1​​);
if(Integer.parseInt(c.getContents())> Integer.parseInt(e.getContents())){
//做某事
}


I wrote a small program on java to read a excel, but how do I modify it so it only prints certain cells?

public class Read
{
    public static void main(String [] args) throws Exception
    {
        File f = new File ("/users/Me/Documents/Test.xls");
        Workbook wb = Workbook.getWorkbook(f);
        Sheet s = wb.getSheet(0);
        int row = s.getRows();
        int col = s.getColumns();

        for(int i = 0; i<row;i++)
        {
            for(int j = 0; j<col;j++)
            {
                Cell c = s.getCell(j,i);
                System.out.print(c.getContents()+"\t");
            }
        }

        System.out.println("");
    }

}

解决方案

If you want to print every 'E' cell then you would want to remove the column loop but keep the row loop and hardcode the index of 'E' into the getCell(x,y) function. As we know 'E' is index 4 in the alphabet therefor if we use the code,

public static void main(String [] args) throws Exception{
    File f = new File ("/users/Me/Documents/Test.xls");
    Workbook wb = Workbook.getWorkbook(f);
    Sheet s = wb.getSheet(0);
    int row = s.getRows();
    int col = s.getColumns();
    for(int i = 0; i<row;i++){
            Cell c = s.getCell(4,i);
            System.out.print(c.getContents()+"\t");
        }
    System.out.println("");
}

Only columns in the 'E' column will be printed.

For your follow up question, how to check if the contents of one cell is greater than the other... Your main problem here is that you are comparing two strings, in order to check if one value is greater than the other you must parse the string to an int (or other data type). In java this can be done by casting or by using methods given to us by datatype functions. An example is seen below where we compare cells C1 and E1.

    Cell c = s.getCell(2,1);
    Cell e = s.getCell(4,1);
    if(Integer.parseInt(c.getContents()) > Integer.parseInt(e.getContents())){
        //Do something
    }

这篇关于如何修改代码,以便仅打印单元格E和F?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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