forked from rongchao/epmet-cloud-rizhao
				
			
				 6 changed files with 398 additions and 23 deletions
			
			
		| @ -0,0 +1,276 @@ | |||
| package com.epmet.commons.tools.utils; | |||
| 
 | |||
| import cn.afterturn.easypoi.excel.ExcelExportUtil; | |||
| import cn.afterturn.easypoi.excel.ExcelImportUtil; | |||
| import cn.afterturn.easypoi.excel.entity.ExportParams; | |||
| import cn.afterturn.easypoi.excel.entity.ImportParams; | |||
| import cn.afterturn.easypoi.excel.entity.TemplateExportParams; | |||
| import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; | |||
| import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.apache.poi.ss.usermodel.Workbook; | |||
| import org.springframework.web.multipart.MultipartFile; | |||
| 
 | |||
| import javax.servlet.http.HttpServletResponse; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.net.URLEncoder; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import java.util.NoSuchElementException; | |||
| 
 | |||
| public class ExcelPoiUtils { | |||
|     /** | |||
|      * excel 导出 | |||
|      * | |||
|      * @param list     数据列表 | |||
|      * @param fileName 导出时的excel名称 | |||
|      * @param response | |||
|      */ | |||
|     public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException { | |||
|         defaultExport(list, fileName, response); | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * 默认的 excel 导出 | |||
|      * | |||
|      * @param list     数据列表 | |||
|      * @param fileName 导出时的excel名称 | |||
|      * @param response | |||
|      */ | |||
|     private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException { | |||
|         //把数据添加到excel表格中
 | |||
|         Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF); | |||
|         downLoadExcel(fileName, response, workbook); | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * excel 导出 | |||
|      * | |||
|      * @param list         数据列表 | |||
|      * @param pojoClass    pojo类型 | |||
|      * @param fileName     导出时的excel名称 | |||
|      * @param response | |||
|      * @param exportParams 导出参数(标题、sheet名称、是否创建表头,表格类型) | |||
|      */ | |||
|     private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) throws IOException { | |||
|         //把数据添加到excel表格中
 | |||
|         Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list); | |||
|         downLoadExcel(fileName, response, workbook); | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * excel 导出 | |||
|      * | |||
|      * @param list         数据列表 | |||
|      * @param pojoClass    pojo类型 | |||
|      * @param fileName     导出时的excel名称 | |||
|      * @param exportParams 导出参数(标题、sheet名称、是否创建表头,表格类型) | |||
|      * @param response | |||
|      */ | |||
|     public static void exportExcel(List<?> list, Class<?> pojoClass, String fileName, ExportParams exportParams, HttpServletResponse response) throws IOException { | |||
|         defaultExport(list, pojoClass, fileName, response, exportParams); | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * excel 导出 | |||
|      * | |||
|      * @param list      数据列表 | |||
|      * @param title     表格内数据标题 | |||
|      * @param sheetName sheet名称 | |||
|      * @param pojoClass pojo类型 | |||
|      * @param fileName  导出时的excel名称 | |||
|      * @param response | |||
|      */ | |||
|     public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) throws IOException { | |||
|         defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName, ExcelType.XSSF)); | |||
|     } | |||
| 
 | |||
| 
 | |||
| 
 | |||
|     /** | |||
|      * excel 导出 | |||
|      * | |||
|      * @param list           数据列表 | |||
|      * @param title          表格内数据标题 | |||
|      * @param sheetName      sheet名称 | |||
|      * @param pojoClass      pojo类型 | |||
|      * @param fileName       导出时的excel名称 | |||
|      * @param isCreateHeader 是否创建表头 | |||
|      * @param response | |||
|      */ | |||
|     public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) throws IOException { | |||
|         ExportParams exportParams = new ExportParams(title, sheetName, ExcelType.XSSF); | |||
|         exportParams.setCreateHeadRows(isCreateHeader); | |||
|         defaultExport(list, pojoClass, fileName, response, exportParams); | |||
|     } | |||
|     /** | |||
|      * 根据模板生成excel后导出 | |||
|      * @param templatePath  模板路径 | |||
|      * @param map 数据集合 | |||
|      * @param fileName 文件名 | |||
|      * @param response | |||
|      * @throws IOException | |||
|      */ | |||
|     public static void exportExcel(TemplateExportParams templatePath, Map<String, Object> map, String fileName, HttpServletResponse response) throws IOException { | |||
|         Workbook workbook = ExcelExportUtil.exportExcel(templatePath, map); | |||
|         downLoadExcel(fileName, response, workbook); | |||
|     } | |||
| 
 | |||
| 
 | |||
|     /** | |||
|      * excel下载 | |||
|      * | |||
|      * @param fileName 下载时的文件名称 | |||
|      * @param response | |||
|      * @param workbook excel数据 | |||
|      */ | |||
|     private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException { | |||
|         try { | |||
|             response.setCharacterEncoding("UTF-8"); | |||
|             response.setHeader("content-Type", "application/vnd.ms-excel"); | |||
|             response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8")); | |||
|             workbook.write(response.getOutputStream()); | |||
|         } catch (Exception e) { | |||
|             throw new IOException(e.getMessage()); | |||
|         } | |||
|     } | |||
| 
 | |||
| 
 | |||
| 
 | |||
|     /** | |||
|      * excel 导入 | |||
|      * | |||
|      * @param file      excel文件 | |||
|      * @param pojoClass pojo类型 | |||
|      * @param <T> | |||
|      * @return | |||
|      */ | |||
|     public static <T> List<T> importExcel(MultipartFile file, Class<T> pojoClass) throws IOException { | |||
|         return importExcel(file, 1, 1, pojoClass); | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * excel 导入 | |||
|      * | |||
|      * @param filePath   excel文件路径 | |||
|      * @param titleRows  表格内数据标题行 | |||
|      * @param headerRows 表头行 | |||
|      * @param pojoClass  pojo类型 | |||
|      * @param <T> | |||
|      * @return | |||
|      */ | |||
|     public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException { | |||
|         if (StringUtils.isBlank(filePath)) { | |||
|             return null; | |||
|         } | |||
|         ImportParams params = new ImportParams(); | |||
|         params.setTitleRows(titleRows); | |||
|         params.setHeadRows(headerRows); | |||
|         params.setNeedSave(true); | |||
|         params.setSaveUrl("/tmp/excel/"); | |||
|         try { | |||
|             return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params); | |||
|         } catch (NoSuchElementException e) { | |||
|             throw new IOException("模板不能为空"); | |||
|         } catch (Exception e) { | |||
|             throw new IOException(e.getMessage()); | |||
|         } | |||
|     } | |||
| 
 | |||
| 
 | |||
|     /** | |||
|      * excel 导入 | |||
|      * | |||
|      * @param file       上传的文件 | |||
|      * @param titleRows  表格内数据标题行 | |||
|      * @param headerRows 表头行 | |||
|      * @param pojoClass  pojo类型 | |||
|      * @param <T> | |||
|      * @return | |||
|      */ | |||
|     public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException { | |||
|         if (file == null) { | |||
|             return null; | |||
|         } | |||
|         try { | |||
|             return importExcel(file.getInputStream(), titleRows, headerRows, pojoClass); | |||
|         } catch (Exception e) { | |||
|             throw new IOException(e.getMessage()); | |||
|         } | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * excel 导入 | |||
|      * | |||
|      * @param inputStream 文件输入流 | |||
|      * @param titleRows   表格内数据标题行 | |||
|      * @param headerRows  表头行 | |||
|      * @param pojoClass   pojo类型 | |||
|      * @param <T> | |||
|      * @return | |||
|      */ | |||
|     public static <T> List<T> importExcel(InputStream inputStream, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException { | |||
|         if (inputStream == null) { | |||
|             return null; | |||
|         } | |||
|         ImportParams params = new ImportParams(); | |||
|         params.setTitleRows(titleRows); | |||
|         params.setHeadRows(headerRows); | |||
|         params.setSaveUrl("/tmp/excel/"); | |||
|         params.setNeedSave(true); | |||
|         try { | |||
|             return ExcelImportUtil.importExcel(inputStream, pojoClass, params); | |||
|         } catch (NoSuchElementException e) { | |||
|             throw new IOException("excel文件不能为空"); | |||
|         } catch (Exception e) { | |||
|             throw new IOException(e.getMessage()); | |||
|         } | |||
|     } | |||
|     /** | |||
|      * excel 导入,有错误信息 | |||
|      * | |||
|      * @param file      上传的文件 | |||
|      * @param pojoClass pojo类型 | |||
|      * @param <T> | |||
|      * @return | |||
|      */ | |||
|     public static <T> ExcelImportResult<T> importExcelMore(MultipartFile file,Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException { | |||
|         if (file == null) { | |||
|             return null; | |||
|         } | |||
|         try { | |||
|             return importExcelMore(file.getInputStream(), titleRows,  headerRows, pojoClass); | |||
|         } catch (Exception e) { | |||
|             throw new IOException(e.getMessage()); | |||
|         } | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * excel 导入 | |||
|      * | |||
|      * @param inputStream 文件输入流 | |||
|      * @param pojoClass   pojo类型 | |||
|      * @param <T> | |||
|      * @return | |||
|      */ | |||
|     private static <T> ExcelImportResult<T> importExcelMore(InputStream inputStream,Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException { | |||
|         if (inputStream == null) { | |||
|             return null; | |||
|         } | |||
|         ImportParams params = new ImportParams(); | |||
|         params.setTitleRows(titleRows);//表格内数据标题行
 | |||
|         params.setHeadRows(headerRows);//表头行
 | |||
|         params.setSaveUrl("/tmp/excel/"); | |||
|         params.setNeedSave(true); | |||
|         params.setNeedVerify(true); | |||
|         try { | |||
|             return ExcelImportUtil.importExcelMore(inputStream, pojoClass, params); | |||
|         } catch (NoSuchElementException e) { | |||
|             throw new IOException("excel文件不能为空"); | |||
|         } catch (Exception e) { | |||
|             throw new IOException(e.getMessage()); | |||
|         } | |||
|     } | |||
| } | |||
| @ -0,0 +1,31 @@ | |||
| package com.epmet.commons.tools.utils; | |||
| 
 | |||
| import cn.afterturn.easypoi.handler.inter.IExcelDataModel; | |||
| import cn.afterturn.easypoi.handler.inter.IExcelModel; | |||
| 
 | |||
| public class ExcelVerifyInfo implements IExcelModel, IExcelDataModel { | |||
| 
 | |||
|     private String errorMsg; | |||
| 
 | |||
|     private int rowNum; | |||
| 
 | |||
|     @Override | |||
|     public Integer getRowNum() { | |||
|         return rowNum; | |||
|     } | |||
| 
 | |||
|     @Override | |||
|     public void setRowNum(Integer rowNum) { | |||
|         this.rowNum = rowNum; | |||
|     } | |||
| 
 | |||
|     @Override | |||
|     public String getErrorMsg() { | |||
|         return errorMsg; | |||
|     } | |||
| 
 | |||
|     @Override | |||
|     public void setErrorMsg(String errorMsg) { | |||
|         this.errorMsg = errorMsg; | |||
|     } | |||
| } | |||
					Loading…
					
					
				
		Reference in new issue