1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| 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.enmus.ExcelType; import org.apache.poi.ss.usermodel.Workbook; import org.springframework.web.multipart.MultipartFile;
public class ExcelUtil {
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); this.defaultExport(list, pojoClass, fileName, response, exportParams); }
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) throws IOException { this.defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName, ExcelType.XSSF)); }
public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException { this.defaultExport(list, fileName, response); }
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) { if (StringUtils.isBlank(filePath)) { return Collections.emptyList(); } ImportParams params = new ImportParams(); params.setTitleRows(titleRows); params.setHeadRows(headerRows); return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params); }
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws Exception { if (file == null) { return Collections.emptyList(); } ImportParams params = new ImportParams(); params.setTitleRows(titleRows); params.setHeadRows(headerRows); return ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params); }
public static <T> List<T> importExcel(MultipartFile file, int sheetIndex, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws Exception { if (file == null) { return Collections.emptyList(); } ImportParams params = new ImportParams(); params.setStartSheetIndex(sheetIndex); params.setTitleRows(titleRows); params.setHeadRows(headerRows); return ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params); }
public static <T> List<T> importExcel(String filePath, int sheetIndex, Integer titleRows, Integer headerRows, Class<T> pojoClass) { ImportParams params = new ImportParams(); params.setStartSheetIndex(sheetIndex); params.setTitleRows(titleRows); params.setHeadRows(headerRows); return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params); }
public static Map<String, Object> createOneSheet(String sheetName, Class<?> clazz, List<?> data) { ExportParams params = new ExportParams("", sheetName, ExcelType.XSSF); return this.createOneSheet(params, clazz, data); }
private static Map<String, Object> createOneSheet(ExportParams params, Class<?> clazz, List<?> data) { Map<String, Object> map = new HashMap<>(8); map.put("title", params); map.put("entity", clazz); map.put("data", data); return map; }
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) throws IOException { Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list); if (workbook != null) { this.downLoadExcel(fileName, response, workbook); } }
private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException { Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.XSSF); if (workbook != null) { this.downLoadExcel(fileName, response, workbook); } }
private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException { response.setCharacterEncoding("UTF-8"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); workbook.write(response.getOutputStream()); } }
|