简体   繁体   English

如何将数据从Excel存储到ArrayList <ArrayList<String> &gt;

[英]How to store data from excel to ArrayList<ArrayList<String>>

I have data from excel like this 我有这样的Excel数据

111 | 222 | 333 | 444 | 555 | 666
11  | 12  | 13  | 14  | 15  | 16
1   | 2   | 3   | 4   | 5   | 6
a   | b   | c   | d   | e   | f

and my code goes like this 我的代码是这样的

public static void main(String[] args) {
    try {
        FileInputStream file = new FileInputStream(new File("xxx"));
        XSSFWorkbook wb = new XSSFWorkbook(file);
        XSSFSheet sheet = wb.getSheetAt(0);

        DataFormatter formatter = new DataFormatter();

        ArrayList<ArrayList<String>> mainArrayList = new ArrayList<ArrayList<String>>();

        ArrayList<String> al = new ArrayList<String>();

        for (int rowNum = 0; rowNum < sheet.getLastRowNum() + 1; rowNum++) {

            String val = null;

            Row r = sheet.getRow(rowNum);

            for (int i = 0; i < r.getLastCellNum() + 1; i++) {
                Cell cell = r.getCell(i);
                val = formatter.formatCellValue(cell);

            }
            al.add(val);
            mainArrayList.add(al);
        }

        System.out.print(mainArrayList);

    } catch (

    Exception e) {
        e.printStackTrace();
    }
}

output: [[, , , ], [, , , ], [, , , ], [, , , ]] 输出: [[, , , ], [, , , ], [, , , ], [, , , ]]

I want the result of would be something like this 我想要的结果是这样的

[[111, 222, 333, 444, 555, 666, ],[11, 12, 13, 14, 15, 16, ],[1, 2, 3, 4, 5, 6, ],[a, s, d, f, g, h, ]]

I believe it is because al.add(val); 我相信是因为al.add(val); is null but I don't know how to do in order for me to add the val into arraylist al. null但我不知道如何将val添加到arraylist al中。 help? 救命?

First you need move ArrayList<String> al = new ArrayList<String>(); 首先,您需要移动ArrayList<String> al = new ArrayList<String>(); into for (int rowNum = 0; rowNum < sheet.getLastRowNum() + 1; rowNum++) {} . 进入for (int rowNum = 0; rowNum < sheet.getLastRowNum() + 1; rowNum++) {} al needs to be new in each loop now. 现在每个循环中都需要更新al

Second, you need move al.add(val); 其次,您需要移动al.add(val); into for (int i = 0; i < r.getLastCellNum() + 1; i++) {} . 进入for (int i = 0; i < r.getLastCellNum() + 1; i++) {} Because in the loop, the val is the right value. 因为在循环中, val是正确的值。

    ArrayList<String> al = new ArrayList<String>();

    for (int rowNum = 0; rowNum < sheet.getLastRowNum() + 1; rowNum++) {

        String val = null;

        Row r = sheet.getRow(rowNum);

        for (int i = 0; i < r.getLastCellNum() + 1; i++) {
            Cell cell = r.getCell(i);
            val = formatter.formatCellValue(cell);

        }
        al.add(val);
        mainArrayList.add(al);
    }

change to this 换成这个

    for (int rowNum = 0; rowNum < sheet.getLastRowNum() + 1; rowNum++) {
        ArrayList<String> al = new ArrayList<String>();
        String val = null;

        Row r = sheet.getRow(rowNum);

        for (int i = 0; i < r.getLastCellNum() + 1; i++) {
            Cell cell = r.getCell(i);
            val = formatter.formatCellValue(cell);
            al.add(val);
        }

        mainArrayList.add(al);
    }

You need to change your code as below 您需要如下更改代码

public static void main(String[] args) {

    try {
      final FileInputStream file =
          new FileInputStream(new File("xxx"));
      final XSSFWorkbook wb = new XSSFWorkbook(file);
      final XSSFSheet sheet = wb.getSheetAt(0);
      final DataFormatter formatter = new DataFormatter();
      final ArrayList<ArrayList<String>> mainArrayList = new ArrayList<ArrayList<String>>();
      for (int rowNum = 0; rowNum < sheet.getLastRowNum() + 1; rowNum++) {
        String val = null;
        final ArrayList<String> al = new ArrayList<String>();
        final Row r = sheet.getRow(rowNum);
        for (int i = 0; i < r.getLastCellNum(); i++) {
          final Cell cell = r.getCell(i);
          val = formatter.formatCellValue(cell);
          al.add(val);
          mainArrayList.add(al);
        }
      }
      System.out.print(mainArrayList);
    } catch (final Exception e) {
      e.printStackTrace();
    }
  }

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

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