0

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?

2 Answers 2

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);
    }
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.