packagecom.common.util;importcn.afterturn.easypoi.excel.ExcelExportUtil;importcn.afterturn.easypoi.excel.ExcelImportUtil;importcn.afterturn.easypoi.excel.entity.ExportParams;importcn.afterturn.easypoi.excel.entity.ImportParams;importcn.afterturn.easypoi.excel.entity.enmus.ExcelType;importorg.apache.commons.lang3.StringUtils;importorg.apache.poi.ss.usermodel.Workbook;importorg.springframework.web.multipart.MultipartFile;importjavax.servlet.http.HttpServletResponse;importjava.io.File;importjava.io.IOException;importjava.net.URLEncoder;importjava.util.List;importjava.util.Map;importjava.util.NoSuchElementException;/*** @Description: 表格工具类*/
public classPoiUtils {public static void exportExcel(List> list, String title, String sheetName, Class> pojoClass, String fileName, booleanisCreateHeader, HttpServletResponse response) {
ExportParams exportParams= newExportParams(title, sheetName);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(list, pojoClass, fileName, response, exportParams);
}public static void exportExcel(List> list, String title, String sheetName, Class>pojoClass, String fileName, HttpServletResponse response) {
defaultExport(list, pojoClass, fileName, response,newExportParams(title, sheetName));
}public static void exportExcel(List>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);
}private static voiddownLoadExcel(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 newRuntimeException(e.getMessage());
}
}private static void defaultExport(List>list, String fileName, HttpServletResponse response) {
Workbook workbook=ExcelExportUtil.exportExcel(list, ExcelType.HSSF);if (workbook != null) ;
downLoadExcel(fileName, response, workbook);
}/*** 导入
*@paramfilePath
*@paramtitleRows
*@paramheaderRows
*@parampojoClass
*@param
*@return
*/
public static List importExcel(String filePath, Integer titleRows, Integer headerRows, ClasspojoClass) {if(StringUtils.isBlank(filePath)) {return null;
}
ImportParams params= newImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List list = null;try{
list= ExcelImportUtil.importExcel(newFile(filePath), pojoClass, params);
}catch(NoSuchElementException e) {throw new RuntimeException("模板不能为空");
}catch(Exception e) {
e.printStackTrace();throw newRuntimeException(e.getMessage());
}returnlist;
}/*** 导入表格
*@paramfile
*@parampojoClass
*@param
*@return
*/
public static List importExcel(MultipartFile file, ClasspojoClass) {if (file == null) {return null;
}
ImportParams params= newImportParams();try{
List list =ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);returnlist;
}catch(NoSuchElementException e) {throw new RuntimeException("excel文件不能为空");
}catch(Exception e) {throw newRuntimeException(e.getMessage());
}
}
}