0
我有一個Excel工作表像分組Excel中值到一個特定的行值
I want output values as
1 - 10
2 - 23
3 - 13
4 - 29
使用集合,我知道我必須收藏的Hashmap但我能弄清楚。
到目前爲止我的代碼
public class Test {
public static void main(String[] args) throws IOException {
String excelFilePath = "C:\\Users\\SINGH\\Desktop\\test.xlsx";
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
ArrayList<Integer> amount = new ArrayList<Integer>();
Map<Integer, ArrayList<Integer>> map = new HashMap<Integer,ArrayList<Integer>>();
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
//System.out.print(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue());
amount.add((int) cell.getNumericCellValue());
}
System.out.print(" - ");
}
System.out.println();
}
System.out.println();
workbook.close();
inputStream.close();
}
}
這裏的問題是,我不能獨立讀取像列標題帳戶和列標題數量這兩個值。 它存儲在一個行對象,然後我迭代througj它是這個問題。如果你看到我如何打印這些值,以及如何知道如果該帳戶的金額存在。 –
您可以檢查單元格的列索引以確定您正在讀取的是哪個值(例如,如果列索引爲0,那麼它必須是accountId,如果列索引爲1,那麼它必須是數量)。您可以使用'getColumnIndex()'獲取列的索引。一旦讀完一行的所有單元格(在遍歷單元格的while循環之後),您可以將我的邏輯放置在地圖內。 –
我想知道你是否可以在這裏使用pojo類,我會嘗試你的想法,但我們可以使用pojo –