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

血红的双手。 提交于 2019-12-11 15:57:22

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/53095040/how-to-store-data-from-excel-to-arraylistarrayliststring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!