简体   繁体   English

使用Java读取Excel工作表的每一行

[英]Reading each row of excel sheet using Java

I have n rows and Columns of Excel sheet . 我有n行和Excel工作表的列。 example

Name OrderId Count Date 
ANC 1234 5 23/3/18 
ABCD 2345 6 23/3/18 
XYGS 3567 7 23/3/18

So in the above data , i want to read the Orderid and Count and give them as input to the next call. 因此,在以上数据中,我想读取Orderid和Count并将它们作为下一个调用的输入。 I have used the below code 我用下面的代码

if(columnIndex != 0) {

  for(Row row1: sheet) {

    Cell C =row1.getCell(columnIndex);
    po_number =c.getStringCellValue();
    excelList.add(po_number);
    int n = columnIndex+1;
    Cell line_number1= row1.getCell(n);
    line_number = line_number1.getStringCellValue();

    FileOutputStream("D:\Users\abcd\Documents\AMD\Output\output.csv"));

    System.out.println(po_number + " " +line_number);

  } 
}

The above code reads the excel sheet and gives the input in console, but i want the particular role to be get assigned in some array or list , from where i can give it as input to next function in iterative manner. 上面的代码读取excel工作表并在控制台中提供输入,但是我希望将特定角色分配到某个数组或列表中,从那里我可以以迭代方式将其作为下一个函数的输入。 Can someone help me with this. 有人可以帮我弄这个吗。

I'd say make a list of arrays of ints (order and count seem to be integers), or String if you want them as Strings 我想说一个整数数组的列表(顺序和计数似乎是整数),或者如果想要它们作为字符串,则为字符串

  List<int[]> data = new ArrayList<>();
  for(int i=1; i<sheet.getPhysicalNumberOfRows(); i++) {
    Row row = sheet.getRow(i);
    int orderId = (int)row.getCell(1).getNumericCellValue();
    int count = (int)row.getCell(2).getNumericCellValue();
    data.add(new int[] { orderId, count });
  }

data should look like this data应如下所示

[1234, 5], [2345, 6], [3567, 7]

And you can pass it to an other method, assuming this method 然后,您可以将其传递给其他方法(假设该方法)

private static void myOtherMethod(List<int[]> data) {
  // Do whatever you want here
}

you call: 您致电:

myOtherMethod(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM