統計表excel導出

一、基於工具類ExcelUtil的exportExcel(String title, String[] headers, Collection<Map<String, Object>> dataset, OutputStream out);java

二、通用方法app

/**
	 * 共用的 導出excel方法
	 * @param fileName excel文件名
	 * @param headList 第一行內容
	 * @param fieldDataList 數據塊
	 * @param response
	 * @throws Exception
	 */
	private void exportExcel(String fileName, List<String> headList, List<Map<String, Object>> fieldDataList,
							 HttpServletResponse response) throws Exception {
		response.setContentType("application/octet-stream");
		response.addHeader("Content-Disposition",
				"attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8") + ";");
		OutputStream out = response.getOutputStream();
		ExcelUtil<List<Map<String, Object>>> excelUtil = new ExcelUtil<>();
		String[] headers = new String[headList.size()];
		headList.toArray(headers);
		excelUtil.exportExcel(fileName, headers, fieldDataList, out);
		out.close();
	}

三、工具

/**
     * 獲取2個日期間 的 全部月份 集合 ,好比 ["2018-01", "2018-02", ……]
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<String> getMonthBetween(String startDate, String endDate) throws ParseException {
        ArrayList<String> result = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化爲年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(startDate));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(endDate));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }
        return result;
    }
public static List<String> convertToEnglishMonth(List<String> months) {
        List<String> englishMonths = new ArrayList<>();
        for(String month : months) {
            englishMonths.add(getEnglishMonth(month));
        }
        return englishMonths;
    }

    /**
     *
     * @param month  2018-03 -> Mar-18
     * @return
     */
    public static String getEnglishMonth(String month) {
        String[] yearAndMonth = month.split("-");
        StringBuilder sb = new StringBuilder();
        switch (yearAndMonth[1]) {
            case "01" : {
            	sb.append("Jan");
            	break;
            }
            case "02" : {
            	sb.append("Feb");
            	break;
            }
            case "03" : {
            	sb.append("Mar");
            	break;
            }
            case "04" : {
            	sb.append("Apr");
            	break;
            }
            case "05" : {
            	sb.append("May");
            	break;
            }
            case "06" : {
            	sb.append("Jun");
            	break;
            }
            case "07" : {
            	sb.append("Jul");
            	break;
            }
            case "08" : {
            	sb.append("Aug");
            	break;
            }
            case "09" : {
            	sb.append("Sep");
            	break;
            }
            case "10" : {
            	sb.append("Oct");
            	break;
            }
            case "11" : {
            	sb.append("Nov");
            	break;
            }
            case "12" : {
            	sb.append("Dec");
            	break;
            }
            default: {}
        }
        sb.append("-").append(yearAndMonth[0].substring(2));
        return sb.toString();
    }
/**
	 * 把日期轉化爲英文,並返回(英文日期)headList
	 * @param monthList     ["2018-04", ……]
	 * @param firstCellName 第一格內容
	 * @return              [" ", "Apr-18", ……]
	 * @throws ParseException
	 */
	private List<String> getHeadEngMonthList(String firstCellName, List<String> monthList, String lastCellName) throws ParseException {
		// 頭headList
		List<String> engMonthList = DateUtil.convertToEnglishMonth(monthList);
		List<String> engHeadList = new ArrayList<>();
		if(StringUtils.isNotEmpty(firstCellName)) {
			engHeadList.add(firstCellName);
		}
		engHeadList.addAll(engMonthList);
		if(StringUtils.isNotEmpty(lastCellName)) {
			engHeadList.add(lastCellName);
		}
		return engHeadList;
	}

四、ui

舉例:.net

基於現有的表結構:這2張表都有user_id、bind_status字段。excel

需求:當月內,客戶既有綁定保單狀態,又有未綁定保單狀態,則一概算做保單用戶。code

相關文章
相關標籤/搜索