简体   繁体   中英

How to store data from excel to ArrayList<ArrayList<String>>

I have data from excel like this

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); is null but I don't know how to do in order for me to add the val into arraylist al. help?

First you need move ArrayList<String> al = new ArrayList<String>(); into for (int rowNum = 0; rowNum < sheet.getLastRowNum() + 1; rowNum++) {} . al needs to be new in each loop now.

Second, you need move al.add(val); into for (int i = 0; i < r.getLastCellNum() + 1; i++) {} . Because in the loop, the val is the right value.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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