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

我在java上编写了一个小程序来读取excel,但是如何修改它以便只打印某些单元格?

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

}

2 个解决方案

#1


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,

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

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.

仅打印“E”列中的列。

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.

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

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

更多相关文章

  1. 使用字符串参数调用AndroidJni静态方法。
  2. java中匹配字符串中的中文字符(含中文标点的)
  3. Java中的数据类型
  4. String字符串的应用
  5. java 正则表达式查找某段字符串中所有小写字母开头的单词并统计
  6. 黑马程序员——Java学习笔记 String类和基本数据类型对象包装类
  7. Javascript 检查字符串是否是数字的几种方法
  8. JavaScript替换字符串中最后一个字符
  9. Java中怎么把字符串数组转为整形数组

随机推荐

  1. Android(安卓)HttpURLConnection及HttpCl
  2. Handler 消息传递机制介绍
  3. 2011.10.12(2)——— android Matrix学习01
  4. 安装Android(安卓)2.2 SDK时出现的问题
  5. Unity3D 调用Android原生方法2
  6. Android四种布局详解
  7. 视频教程-OpenCV人脸标定技术实战 适合iO
  8. Android(安卓)adb push ... Read-only fi
  9. Android界面五种常用布局方式
  10. android创建文件夹和文件