基本信息
源码名称:java实现Excel导出
源码大小:0.02M
文件格式:.docx
开发语言:Java
更新时间:2021-09-10
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

导入导出


/**  * 导出  *  * @param list  * @param title  * @param sheetName  * @param pojoClass  * @param fileName  * @param isCreateHeader  * @param response  */ public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) {
    ExportParams exportParams = new ExportParams(title, sheetName);  exportParams.setCreateHeadRows(isCreateHeader);  defaultExport(list, pojoClass, fileName, response, exportParams);  } /**  * 导出  *  * @param list  * @param title  * @param sheetName  * @param pojoClass  * @param fileName  * @param response  */ public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) { defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName)); } /**  * 导出excel  *  * @param list  * @param fileName  * @param response  */ public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) { defaultExport(list, fileName, 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); } /**  * 下载  *  * @param fileName  * @param response  * @param 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) {
    }
} private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
    Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);  if (workbook != null) ;  downLoadExcel(fileName, response, workbook); } /**  * 导入  *  * @param filePath  * @param titleRows  * @param headerRows  * @param pojoClass  * @param <T>  * @return  */ 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 e;  } catch (Exception e) {
        e.printStackTrace();  throw e;  } return list; } /**  * 导入  *  * @param file 文件  * @param titleRows 表头  * @param headerRows 头部列  * @param pojoClass  * @param <T>  * @return  */ 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 e;  } catch (Exception e) {
        e.printStackTrace();  } return list; } public static <T> ExcelImportResult<T> importResultExcel(MultipartFile file, Class<T> pojoClass) { if (file == null) { return null;  }
    ImportParams params = new ImportParams();  params.setTitleRows(0);  params.setHeadRows(1);  ExcelImportResult<T> importResult = null;  try {
        importResult = ExcelImportUtil.importExcelMore(file.getInputStream(), pojoClass, params);  } catch (NoSuchElementException e) { throw e;  } catch (Exception e) {
        e.printStackTrace();  } return importResult; } /**  * 根据excel模板直接进行导出下载  * @param response  * @param templatePath  * @param fileName  * @param map  */ public static void exportExcel(HttpServletResponse response,String templatePath,String fileName,Map map){ try{
        TemplateExportParams params = new TemplateExportParams(templatePath);  Workbook workbook = ExcelExportUtil.exportExcel(params, map);  response.reset();  response.setHeader("Content-Disposition", "attachment;filename*= UTF-8''" URLEncoder.encode(fileName,"UTF-8") ".xlsx");  //response.setHeader("Content-Disposition", "attachment;filename="   fileName ".xlsx");  response.setContentType("application/vnd.ms-excel;charset=UTF-8");  response.setCharacterEncoding("UTF-8");  OutputStream output = response.getOutputStream();  workbook.write(output);  workbook.close();  output.close();  }catch (Exception e){
        e.printStackTrace();  }
}