问题描述
- 小白求教,分日期求和问题
-
想实现分日期累加求和并添加到excel中, 可是传到excel中的数据把每一天的数据都打出来了。帮忙看一下问题出在哪了? double sum = 0; double a = 0; String cellValue = """"; SimpleDateFormat formatter = new SimpleDateFormat(""yyyy/MM/dd""); Date date = inputRow.getCell( Integer.parseInt(source.getProperty(""日期列"")) - 1) .getDateCellValue(); cellValue = formatter.format(date); ArrayList<String> date1 = new ArrayList<String>(); ArrayList<Double> value1 = new ArrayList<Double>(); ArrayList<String> date2 = new ArrayList<String>(); ArrayList<Double> value2 = new ArrayList<Double>(); value1.add(inputRow.getCell( Integer.parseInt(source.getProperty(""求和列"")) - 1) .getNumericCellValue()); date1.add(cellValue); for (int c = 0; c < date1.size(); c++) { try { if (date1.get(c).equals(date1.get(c + 1))) { a = value1.get(c); sum = a + sum; } else { a = value1.get(c); sum = a + sum; date2.add(date1.get(c)); value2.add(sum); sum = 0.0; } } catch (Exception e) { a = value1.get(c); sum = a + sum; date2.add(date1.get(c)); value2.add(sum); } } for (int k = 0; k < date2.size(); k++) { outputRow.createCell(14).setCellValue(value2.get(k)); outputRow.createCell(3).setCellValue(date2.get(k)); outputRow.createCell(4).setCellValue(date2.get(k)); }}主函数里的方法,调用的processSum求和方法for (i = o; i < lastnum; i++) { XSSFRow readrow = readsheet.getRow(i);// 获取输入行 Process p = new ProcessRow(); if (p.panduan2(readrow)) { XSSFSheet sheet = sc.getSheetAt(0); XSSFRow outputrow = sheet.createRow(j++); p.processSum(readrow outputrow); } }
解决方案
定义hashmap,先循环,把相同日期的累加到相同日期为key的数量中,然后再遍历输出,就是分组的结果了。
时间: 2024-12-22 14:06:45