|
| 1 | +package com.chen.util; |
| 2 | + |
| 3 | +import cn.afterturn.easypoi.excel.ExcelExportUtil; |
| 4 | +import cn.afterturn.easypoi.excel.ExcelImportUtil; |
| 5 | +import cn.afterturn.easypoi.excel.entity.ExportParams; |
| 6 | +import cn.afterturn.easypoi.excel.entity.ImportParams; |
| 7 | +import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; |
| 8 | +import com.sun.deploy.net.URLEncoder; |
| 9 | +import org.apache.commons.lang3.StringUtils; |
| 10 | +import org.apache.poi.ss.usermodel.Workbook; |
| 11 | +import org.springframework.web.multipart.MultipartFile; |
| 12 | + |
| 13 | +import javax.servlet.http.HttpServletResponse; |
| 14 | +import java.io.File; |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.NoSuchElementException; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author chen weijie |
| 22 | + * @date 2018-01-19 12:22 AM |
| 23 | + */ |
| 24 | +public class FileUtil { |
| 25 | + public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response){ |
| 26 | + ExportParams exportParams = new ExportParams(title, sheetName); |
| 27 | + exportParams.setCreateHeadRows(isCreateHeader); |
| 28 | + defaultExport(list, pojoClass, fileName, response, exportParams); |
| 29 | + |
| 30 | + } |
| 31 | + public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName, HttpServletResponse response){ |
| 32 | + defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName)); |
| 33 | + } |
| 34 | + public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){ |
| 35 | + defaultExport(list, fileName, response); |
| 36 | + } |
| 37 | + |
| 38 | + private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) { |
| 39 | + Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list); |
| 40 | + if (workbook != null); |
| 41 | + downLoadExcel(fileName, response, workbook); |
| 42 | + } |
| 43 | + |
| 44 | + private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook){ |
| 45 | + try { |
| 46 | + response.setCharacterEncoding("UTF-8"); |
| 47 | + response.setHeader("content-Type", "application/vnd.ms-excel"); |
| 48 | + response.setHeader("Content-Disposition", |
| 49 | + "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); |
| 50 | + workbook.write(response.getOutputStream()); |
| 51 | + } catch (IOException e) { |
| 52 | +// throw new IOException(e.getMessage()); |
| 53 | + } |
| 54 | + } |
| 55 | + private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) { |
| 56 | + Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF); |
| 57 | + if (workbook != null); |
| 58 | + downLoadExcel(fileName, response, workbook); |
| 59 | + } |
| 60 | + |
| 61 | + public static <T> List<T> importExcel(String filePath,Integer titleRows,Integer headerRows, Class<T> pojoClass){ |
| 62 | + if (StringUtils.isBlank(filePath)){ |
| 63 | + return null; |
| 64 | + } |
| 65 | + ImportParams params = new ImportParams(); |
| 66 | + params.setTitleRows(titleRows); |
| 67 | + params.setHeadRows(headerRows); |
| 68 | + List<T> list = null; |
| 69 | + try { |
| 70 | + list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params); |
| 71 | + }catch (NoSuchElementException e){ |
| 72 | +// throw new NormalException("模板不能为空"); |
| 73 | + } catch (Exception e) { |
| 74 | + e.printStackTrace(); |
| 75 | +// throw new NormalException(e.getMessage()); |
| 76 | + } |
| 77 | + return list; |
| 78 | + } |
| 79 | + public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){ |
| 80 | + if (file == null){ |
| 81 | + return null; |
| 82 | + } |
| 83 | + ImportParams params = new ImportParams(); |
| 84 | + params.setTitleRows(titleRows); |
| 85 | + params.setHeadRows(headerRows); |
| 86 | + List<T> list = null; |
| 87 | + try { |
| 88 | + list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params); |
| 89 | + }catch (NoSuchElementException e){ |
| 90 | +// throw new NormalException("excel文件不能为空"); |
| 91 | + } catch (Exception e) { |
| 92 | +// throw new NormalException(e.getMessage()); |
| 93 | + } |
| 94 | + return list; |
| 95 | + } |
| 96 | +} |
0 commit comments