多页签导出

1.前端js

var datas = actives.getTableSelectedData()
if(datas&&datas.length>0){
    $.each(datas,function (index,value) {
        window.open(urls.listExport+"?declNo="+value.declNo + "&t="+index);
    })
}else {
    view.msg("请至少选择一行数据!")
}

2.后台逻辑

@RequestMapping(value = "/list_export")
public void ListExport(String declNo,HttpServletResponse response){
    try {
        Map<String,Object> data = new HashMap<>();
        DecHead decHead = decHeadAppService.loadDecHeadByDeclNo(declNo);
        String ieFlag = decHead.getIeFlag();
        List<DecList> decLists = decListAppService.listDecListInfoByDeclNo(declNo);
        data.put("head",decHead);
        data.put("list",decLists);
        List<DecContainer> containers = decContainAppService.listDecContainerBydeclNo(declNo);
        List<DecLicenseDocu> decLicenseDocus = decLicenseDocuAppService.listDecLicenseDocuBydeclNo(declNo);
        Map<String,Object> containerMap = new HashMap<>();
        Map<String,Object> docuMap = new HashMap<>();
        containerMap.put("list",containers);
        docuMap.put("list",decLicenseDocus);
        Map<Integer,Map<String,Object>> mapMap = new HashMap<>();
        mapMap.put(0,data);
        mapMap.put(1,containerMap);
        mapMap.put(2,docuMap);

         /*获取模板路径*/
        URL url =   ResourceUtils.getURL("classpath:exceltemplet/decListExport_"+ieFlag+".xls");
        FileUtils.downloadTemplates(url.getPath(),mapMap,"报关单明细"+declNo,response);
    }catch (Exception e){
        log.error(e.getMessage(),e);
        e.printStackTrace();
    }
}

3.工具类

public static void downloadTemplates(String path, Map<Integer, Map<String, Object>> map, String fileName, HttpServletResponse response) {
    try {

        logger.info("路径:" + path);
        path = path.replaceAll("%20", " ");
        /*设置模板路径*/
        TemplateExportParams params = new TemplateExportParams(path, true);
        if (map == null) {
            map = new HashMap<>();
        }
        /*加载模板*/
        Workbook workbook = new ExcleExportUtil().createExcleByTemplate(params, map);
        /*设置返回表头*/
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Content-Disposition",
            "attachment;filename=" + URLEncoder.encode(fileName + ".xls", "UTF-8"));
        workbook.write(response.getOutputStream());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

4.excel模板

发布了68 篇原创文章 · 获赞 19 · 访问量 24万+

猜你喜欢

转载自blog.csdn.net/tyjlearning/article/details/102775191