easypoi多sheet导出

public class ExcelUtil {

    public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, ExcelType type, HttpServletResponse response) {
        ExportParams exportParams = new ExportParams(title, sheetName,type);
        defaultExport(list, pojoClass, fileName, response, exportParams);

    }


    public static void exportExcel(List<Map<String, Object>> list, String fileName,ExcelType type, HttpServletResponse response) {
        defaultExport(list, fileName, type,response);
    }

    private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
        if (workbook != null) ;
        downLoadExcel(fileName, response, workbook);
    }

    private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    private static void defaultExport(List<Map<String, Object>> list, String fileName,ExcelType type, HttpServletResponse response) {
        Workbook workbook = ExcelExportUtil.exportExcel(list, type);
        if (workbook != null) ;
        downLoadExcel(fileName, response, workbook);
    }

    public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
        if (StringUtils.isBlank(filePath)) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
        } catch (NoSuchElementException e) {
            throw new RuntimeException("模板不能为空");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
        return list;
    }

    public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
        if (file == null) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
        } catch (NoSuchElementException e) {
            throw new RuntimeException("excel文件不能为空");
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return list;
    }

    public static void exportMultiSheetExcel(List<ExportView> list,  String fileName, ExcelType type,HttpServletResponse response) {
        List<Map<String,Object>> excel  = new ArrayList<>();
        for(ExportView view :list){
            Map<String,Object> sheet = new HashMap<>();
            sheet.put("title",view.getExportParams());
            sheet.put("entity",view.getCls());
            sheet.put("data",view.getDataList());
            excel.add(sheet);
        }
        exportExcel(excel,fileName,type,response);
    }


    public static class ExportView {

        public ExportView(){

        }
        public ExportView(String sheetName,List<?> dataList,ExcelType type ,Class<?> cls){
            ExportParams exportParams = new ExportParams(null, sheetName,type);
            exportParams.setCreateHeadRows(false);
            this.exportParams = exportParams;
            this.dataList = dataList ;
            this.cls = cls ;
        }

        private ExportParams exportParams;
        private List<?> dataList;
        private Class<?> cls;

        public ExportParams getExportParams() {
            return exportParams;
        }
        public void setExportParams(ExportParams exportParams) {
            this.exportParams = exportParams;
        }

        public Class<?> getCls() {
            return cls;
        }
        public void setCls(Class<?> cls) {
            this.cls = cls;
        }
        public List<?> getDataList() {
            return dataList;
        }
        public void setDataList(List<?> dataList) {
            this.dataList = dataList;
        }

    }




}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容