Reputation: 306
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?
Upvotes: 0
Views: 1037
Reputation: 1001
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();
}
}
Upvotes: 1
Reputation: 1198
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);
}
Upvotes: 1