Java 将Excel值分组到特定行值

Java 将Excel值分组到特定行值,java,apache-poi,Java,Apache Poi,我有一张Excel表格,比如 使用集合,我知道我必须将集合映射到Hashmap,但我能够弄清楚 到目前为止我的代码 public class Test { public static void main(String[] args) throws IOException { String excelFilePath = "C:\\Users\\SINGH\\Desktop\\test.xlsx"; FileInputStream inputStream = new FileI

我有一张Excel表格,比如

使用集合,我知道我必须将集合映射到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();
}

}
公共类测试{
公共静态void main(字符串[]args)引发IOException{
String excelFilePath=“C:\\Users\\SINGH\\Desktop\\test.xlsx”;
FileInputStream inputStream=新FileInputStream(新文件(excelFilePath));
工作簿=新XSSF工作簿(inputStream);
Sheet firstSheet=工作簿。getSheetAt(0);
迭代器迭代器=firstSheet.Iterator();
ArrayList金额=新ArrayList();
Map Map=newhashmap();
while(iterator.hasNext()){
行nextRow=iterator.next();
迭代器cellIterator=nextRow.cellIterator();
while(cellIterator.hasNext()){
Cell=cellIterator.next();
开关(cell.getCellType()){
case Cell.Cell\u类型\u字符串:
//System.out.print(cell.getStringCellValue());
打破
case Cell.Cell\u类型\u数值:
System.out.print(cell.getNumericCellValue());
amount.add((int)cell.getNumericCellValue());
}
系统输出打印(“-”);
}
System.out.println();
}
System.out.println();
workbook.close();
inputStream.close();
}
}

我们可以使用地图来存储帐户id和相应的金额。并且,在迭代excel工作表时,如果已经存在一个值,我们可以将现有值添加到该值中,然后再次存储它。下面是示例代码:

Map<Integer, Integer> accounts = new LinkedHashMap<Integer, Integer>(); 
int accountId = //read account id from excel
int amount = //read amount from excel
//If entry already exists in map then, add the new value to existing value
if(accounts.containsKey(accountId)){
    amount += accounts.get(accountId);
}
//set the value in map, this will replace the existing value
accounts.put(accountId, amount);
然后,我们可以使用列表来存储Dto对象。我们还需要使用方法检查dto是否已经存在于列表中(使用accountId),如果是,那么我们可以添加金额。以下是pseido代码:

List<AccountDto> accounts = new ArrayList<AccountDto>();
int accountId = //read account id from excel
int amount = //read amount from excel

AccountDto accountDto = findById(accountId, accounts);
//Check whether account exists in the list
if(null == accountDto){
    //If not then add a new object
    accountDto = new AccountDto(accountId, amount);
}else{
   //If yes then, add the amount
    accountDto.setAmount(accountDto.getAmount() + amount);
}
List accounts=new ArrayList();
int accountId=//从excel读取帐户id
int amount=//从excel读取金额
AccountDto AccountDto=findById(accountId,accounts);
//检查列表中是否存在帐户
if(null==accountDto){
//如果没有,则添加一个新对象
accountDto=新的accountDto(accountId,金额);
}否则{
//如果是,则添加金额
accountDto.setAmount(accountDto.getAmount()+金额);
}

希望有帮助。

这里的问题是,我无法独立读取列标题帐户和列标题金额的值。它存储在一个行对象中,然后我对它进行迭代,这就是问题所在。如果您看到我如何打印这些值,以及如何知道帐户是否存在金额。您可以检查单元格的列索引以确定您正在读取的值(例如,如果列索引为0,则它必须是accountId,如果列索引为1,则它必须是amount)。您可以使用
getColumnIndex()
获取列的索引。一旦您完成了对一行的所有单元格的读取(在迭代单元格的while循环之后),您可以将我的逻辑放入映射中。我想知道您是否可以在这里使用pojo类,我会尝试您的想法,但我们可以使用pojo吗?我们显然可以使用pojo类。但是,如果我们将类对象存储到列表中,那么我们需要编写方法,例如,从列表中返回与提供的ac对应的特定对象地图已经有了,因此,我们的任务更容易:)你能给我举个例子吗,我是新手
class AccountDto{
    private int accountId;
    private int amount;

    //getters and setters
}
List<AccountDto> accounts = new ArrayList<AccountDto>();
int accountId = //read account id from excel
int amount = //read amount from excel

AccountDto accountDto = findById(accountId, accounts);
//Check whether account exists in the list
if(null == accountDto){
    //If not then add a new object
    accountDto = new AccountDto(accountId, amount);
}else{
   //If yes then, add the amount
    accountDto.setAmount(accountDto.getAmount() + amount);
}