【问题标题】:Reading each row of excel sheet using Java使用Java读取每一行excel表
【发布时间】:2018-03-26 07:05:38
【问题描述】:

我有 n 行和列的 Excel 表。例子

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

所以在上面的数据中,我想读取 Orderid 和 Count 并将它们作为下一次调用的输入。我使用了下面的代码

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

  } 
}

上面的代码读取 excel 表并在控制台中提供输入,但我希望将特定角色分配到某个数组或列表中,从那里我可以将其作为输入以迭代方式提供给下一个函数。有人可以帮我解决这个问题吗?

【问题讨论】:

  • 您的问题不清楚。您想创建一个数组并存储您现在正在打印的内容吗?

标签: java iterator row excel-reader


【解决方案1】:

我会说创建一个整数数组列表(顺序和计数似乎是整数),或者如果您希望它们作为字符串,则为字符串

  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 应该是这样的

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

你可以将它传递给另一个方法,假设是这个方法

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

你打电话:

myOtherMethod(data);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    相关资源
    最近更新 更多