下载模板

下载模板

前端页面代码



//下载模板
function downLoadDataTemplate() {
    var url = getUrl('/auditRealNameLie/down_template_csv', token);
    window.document.forms[0].action = url;
    window.document.forms[0].target = "_self";
    window.document.forms[0].method = "POST";
    window.document.forms[0].submit();
}

后端实现

/**
* 表头
*/
private String[] cloArr = {"用户号码", "导入日期", "类型", "数据来源", "处理时限"};

/**
 * 下载csv模板
 *
 * @param request
 * @param response
 */
@RequestMapping(value = "/down_template_csv", method = RequestMethod.POST)
public void down_template_csv(HttpServletRequest request, HttpServletResponse response) {
    response.setContentType("text/plain");
    response.addHeader("Content-Disposition", "attachment;filename=auditRealNameLie.csv");
    BufferedOutputStream buff = null;
    StringBuffer sb = new StringBuffer();
    ServletOutputStream out = null;
    try {
        out = response.getOutputStream();
        buff = new BufferedOutputStream(out);
        for (String s : cloArr) {
            sb.append(s + ",");
        }
        buff.write(sb.toString().getBytes(CHARSET));
        buff.flush();
        buff.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

下载Excel模板

/**
 * 下载建表模板
 */
@RequestMapping(value = "/down_template", method = RequestMethod.POST)
public void down_template(HttpServletResponse res) {
    List colList = new ArrayList();
    colList.add("业务号码");
    colList.add("电路端号");
    colList.add("业务类型");

    //定义表头
    List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
    for (int i = 0; i < colList.size(); i++) {
        String key = colList.get(i).toString();
        String name = colList.get(i).toString();
        entity.add(new ExcelExportEntity(name, key, 15));
    }

    //组装数据(下载模板只展示表头,可将list作为空数据传递)
    List<Map<String, Object>> Reslist = new ArrayList<Map<String, Object>>();

    ExportParams params = new ExportParams();
    params.setSheetName("上传模板");
    Workbook workbook = ExcelExportUtil.exportExcel(params, entity, Reslist);
    ServletOutputStream ot = null;
    try {
        //准备文件名称
        String fileName = "上传模板";
        res.setContentType("application/ms-excel");
        res.setCharacterEncoding("utf-8");
        String contentDispositionValue = "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1") + ".xls";
        res.setHeader("Content-Disposition", contentDispositionValue);
        ot = res.getOutputStream();
        workbook.write(ot);
        byte[] bytes = new byte[4096];
        ot.write(bytes);
        if (ot != null) {
            ot.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

你可能感兴趣的:(下载模板)