try {
BizTargetsurvey targetsurvey = bizTargetsurveyMapper.selectBizTargetsurveyById(targetsurveyid);
Map<String, Object> params = new HashMap<String, Object>();
params.put("targetsurveyid", targetsurveyid);
List<Map<String, Object>> scores = bizTargetsurveyDetailMapper.listSurveyDetailWithScore(params);
Map<String, Set<String>> kcmbMap =
scores.stream().collect(Collectors.groupingBy(o -> o.get("targettitle").toString(),
Collectors.mapping(o -> o.get("tmno").toString(), Collectors.toSet())));
Map<String, List<Map<String, Object>>> scoreMap =
scores.stream().collect(Collectors.groupingBy(o -> o.get("userid").toString()));
List<Map<String, Double>> wrapScoreList = new ArrayList<Map<String, Double>>();
scoreMap.forEach((k, v) -> {
Map<String, Double> smap = new HashMap<String, Double>();
for (Map<String, Object> sm : v) {
smap.put("Q" + sm.get("tmno").toString(), Double.valueOf(sm.get("score").toString()));
}
wrapScoreList.add(smap);
});
Map<String, Double> averages = new HashMap<String, Double>();
Map<String, Double> sums = new HashMap<String, Double>();
kcmbMap.forEach((k, v) -> {
double sum = 0;
for (String qt : v) {
double avg = wrapScoreList.stream().mapToDouble(o -> o.get("Q" + qt)).average().getAsDouble();
averages.put("Q" + qt, avg);
sum += avg;
}
sums.put(k, sum);
});
// List<Double> Q1List = wrapScoreList.stream().map(o -> o.get("Q1")).collect(Collectors.toList());
// List<Double> Q2List = wrapScoreList.stream().map(o -> o.get("Q2")).collect(Collectors.toList());
// System.out.println(Q1List);
System.out.println(averages);
System.out.println(sums);
SXSSFWorkbook wb = new SXSSFWorkbook(500);
SXSSFSheet sheet = wb.createSheet();
wb.setSheetName(0, targetsurvey.getTitle() + "统计");
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
CellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
Font titleFont = wb.createFont();
titleFont.setFontName("Arial");
titleFont.setFontHeightInPoints((short)16);
titleFont.setBold(true);
style.setFont(titleFont);
styles.put("title", style);
style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
Font dataFont = wb.createFont();
dataFont.setFontName("Arial");
dataFont.setFontHeightInPoints((short)10);
style.setFont(dataFont);
styles.put("data", style);
style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data"));
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font headerFont = wb.createFont();
headerFont.setFontName("Arial");
headerFont.setFontHeightInPoints((short)10);
headerFont.setBold(true);
headerFont.setColor(IndexedColors.WHITE.getIndex());
style.setFont(headerFont);
styles.put("header", style);
Set<Entry<String, Set<String>>> entrySet = kcmbMap.entrySet();
int startCol = 1;
SXSSFRow row0 = sheet.createRow(0);
// 编号
SXSSFRow row1 = sheet.createRow(1);
Cell row1cell = row1.createCell(0);
row1cell.setCellValue("编号");
row1cell.setCellStyle(styles.get("data"));
int i = 1;
for (Entry<String, Set<String>> entry : entrySet) {
if (startCol < (startCol + entry.getValue().size() - 1)) {
CellRangeAddress cellRangeAddress =
new CellRangeAddress(0, 0, startCol, startCol + entry.getValue().size() - 1);
sheet.addMergedRegion(cellRangeAddress);
}
Cell cell = row0.createCell(startCol);
cell.setCellValue(entry.getKey());
cell.setCellStyle(styles.get("data"));
Iterator<String> vit = entry.getValue().iterator();
Cell row1celli = null;
while (vit.hasNext()) {
row1celli = row1.createCell(i);
row1celli.setCellValue("Q" + vit.next());
row1celli.setCellStyle(styles.get("data"));
i++;
}
// System.out.println("startCol: " + startCol + " endCol: " + (startCol + entry.getValue().size() - 1));
startCol += entry.getValue().size();
}
int qCelli = 1;
SXSSFRow qRow = null;
SXSSFCell qRowCelli = null;
for (Map<String, Double> ws : wrapScoreList) {
qRow = sheet.createRow(sheet.getLastRowNum() + 1);
qRowCelli = qRow.createCell(0);
qRowCelli.setCellStyle(styles.get("data"));
qRowCelli.setCellValue(qCelli);
for (int qColi = 1; qColi <= ws.size(); qColi++) {
qRowCelli = qRow.createCell(qColi);
qRowCelli.setCellStyle(styles.get("data"));
qRowCelli.setCellValue(ws.get(sheet.getRow(1).getCell(qColi).getStringCellValue()));
}
qCelli++;
}
//
wb.write(response.getOutputStream());
IOUtils.closeQuietly(wb);
} catch (Exception e) {
e.printStackTrace();
}
未经允许不得转载:
红吉他 »
JAVA »
java8 Stream分组以及poi对excel的写入