您好,登錄后才能下訂單哦!
這篇文章運用簡單易懂的例子給大家介紹SpringBoot中EasyExcel如何實現Excel文件的導入導出,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
Easyexcel
Easyexcel 是阿里巴巴的開源項目,用來優化Excel
文件處理過程:
Excel
比較有名的框架有Apache poi
、jxl
。但他們都存在一個嚴重的問題就是非常的耗內存,poi
有一套SAX
模式的API
可以一定程度的解決一些內存溢出的問題,但poi
還是有一些缺陷,比如07版Excel
解壓縮以及解壓后存儲都是在內存中完成的,內存消耗依然很大。poi
對07版Excel
的解析,能夠原本一個3M的excel
用POI sax
依然需要100M左右內存降低到幾M,并且再大的excel
不會出現內存溢出。SpringBoot+ EasyExcel實現Excel文件的導入導出
導入依賴
<!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.2</version> <optional>true</optional> </dependency> <!--easyExcel--> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>1.1.2-beat1</version> </dependency> <!--fastjson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <exclusions> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </dependency>
為了防止Excel文件被破壞在pom.xml
添加以下內容
<build> <plugins> <!-- 讓maven不編譯xls文件,但仍將其打包 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <nonFilteredFileExtensions> <nonFilteredFileExtension>xls</nonFilteredFileExtension> <nonFilteredFileExtension>xlsx</nonFilteredFileExtension> </nonFilteredFileExtensions> </configuration> </plugin> </plugins> </build>
application.propertis:配置文件
#temp files project.tmp.files.path=/Users/mac/Desktop/image/tmp/files/
在SpringBoot啟動類添加臨時文件設置
@Value("${project.tmp.files.path}") public String filesPath; @Bean MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); //設置路徑xxx factory.setLocation(filesPath); return factory.createMultipartConfig(); }
ExcelUtil:Excel工具類
@Slf4j public class ExcelUtil { private static Sheet initSheet; static { initSheet = new Sheet(1, 0); initSheet.setSheetName("sheet"); //設置自適應寬度 initSheet.setAutoWidth(Boolean.TRUE); } public static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) { try { response.setCharacterEncoding("UTF-8"); response.setContentType("application/octet-stream;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); workbook.write(response.getOutputStream()); } catch (IOException e) { // throw new NormalException(e.getMessage()); } } /** * 讀取少于1000行數據 * * @param filePath 文件絕對路徑 * @return */ public static List<Object> readLessThan1000Row(String filePath) { return readLessThan1000RowBySheet(filePath, null); } /** * 讀小于1000行數據, 帶樣式 * filePath 文件絕對路徑 * initSheet : * sheetNo: sheet頁碼,默認為1 * headLineMun: 從第幾行開始讀取數據,默認為0, 表示從第一行開始讀取 * clazz: 返回數據List<Object> 中Object的類名 */ public static List<Object> readLessThan1000RowBySheet(String filePath, Sheet sheet) { if (!StringUtils.hasText(filePath)) { return null; } sheet = sheet != null ? sheet : initSheet; InputStream fileStream = null; try { fileStream = new FileInputStream(filePath); return EasyExcelFactory.read(fileStream, sheet); } catch (FileNotFoundException e) { log.info("找不到文件或文件路徑錯誤, 文件:{}", filePath); } finally { try { if (fileStream != null) { fileStream.close(); } } catch (IOException e) { log.info("excel文件讀取失敗, 失敗原因:{}", e); } } return null; } /** * 讀大于1000行數據 * * @param filePath 文件覺得路徑 * @return */ public static List<Object> readMoreThan1000Row(String filePath) { return readMoreThan1000RowBySheet(filePath, null); } /** * 讀大于1000行數據, 帶樣式 * * @param filePath 文件覺得路徑 * @return */ public static List<Object> readMoreThan1000RowBySheet(String filePath, Sheet sheet) { if (!StringUtils.hasText(filePath)) { return null; } sheet = sheet != null ? sheet : initSheet; InputStream fileStream = null; try { fileStream = new FileInputStream(filePath); ExcelListener excelListener = new ExcelListener(); EasyExcelFactory.readBySax(fileStream, sheet, excelListener); return excelListener.getDatas(); } catch (FileNotFoundException e) { log.error("找不到文件或文件路徑錯誤, 文件:{}", filePath); } finally { try { if (fileStream != null) { fileStream.close(); } } catch (IOException e) { log.error("excel文件讀取失敗, 失敗原因:{}", e); } } return null; } /** * 讀大于1000行數據, 帶樣式 * * @return */ public static List<Object> readMoreThan1000RowBySheetFromInputStream(InputStream inputStream, Sheet sheet) { sheet = sheet != null ? sheet : initSheet; InputStream fileStream = null; ExcelListener excelListener = new ExcelListener(); EasyExcelFactory.readBySax(inputStream, sheet, excelListener); return excelListener.getDatas(); } /** * 生成excle * * @param filePath 絕對路徑 * @param data 數據源 * @param head 表頭 */ public static void writeBySimple(String filePath, List<List<Object>> data, List<String> head) { writeSimpleBySheet(filePath, data, head, null); } /** * 生成excle * * @param filePath 路徑 * @param data 數據源 * @param sheet excle頁面樣式 * @param head 表頭 */ public static void writeSimpleBySheet(String filePath, List<List<Object>> data, List<String> head, Sheet sheet) { sheet = (sheet != null) ? sheet : initSheet; if (head != null) { List<List<String>> list = new ArrayList<>(); head.forEach(h -> list.add(Collections.singletonList(h))); sheet.setHead(list); } OutputStream outputStream = null; ExcelWriter writer = null; try { outputStream = new FileOutputStream(filePath); writer = EasyExcelFactory.getWriter(outputStream); writer.write1(data, sheet); } catch (FileNotFoundException e) { log.error("找不到文件或文件路徑錯誤, 文件:{}", filePath); } finally { try { if (writer != null) { writer.finish(); } if (outputStream != null) { outputStream.close(); } } catch (IOException e) { log.error("excel文件導出失敗, 失敗原因:{}", e); } } } /** * 生成excle * * @param filePath 路徑 * @param data 數據源 */ public static void writeWithTemplate(String filePath, List<? extends BaseRowModel> data) { writeWithTemplateAndSheet(filePath, data, null); } /** * 生成excle * * @param filePath 路徑 * @param data 數據源 * @param sheet excle頁面樣式 */ public static void writeWithTemplateAndSheet(String filePath, List<? extends BaseRowModel> data, Sheet sheet) { if (CollectionUtils.isEmpty(data)) { return; } sheet = (sheet != null) ? sheet : initSheet; sheet.setClazz(data.get(0).getClass()); OutputStream outputStream = null; ExcelWriter writer = null; try { outputStream = new FileOutputStream(filePath); writer = EasyExcelFactory.getWriter(outputStream); writer.write(data, sheet); } catch (FileNotFoundException e) { log.error("找不到文件或文件路徑錯誤, 文件:{}", filePath); } finally { try { if (writer != null) { writer.finish(); } if (outputStream != null) { outputStream.close(); } } catch (IOException e) { log.error("excel文件導出失敗, 失敗原因:{}", e); } } } /** * 生成多Sheet的excle * * @param filePath 路徑 * @param multipleSheelPropetys */ public static void writeWithMultipleSheel(String filePath, List<MultipleSheelPropety> multipleSheelPropetys) { if (CollectionUtils.isEmpty(multipleSheelPropetys)) { return; } OutputStream outputStream = null; ExcelWriter writer = null; try { outputStream = new FileOutputStream(filePath); writer = EasyExcelFactory.getWriter(outputStream); for (MultipleSheelPropety multipleSheelPropety : multipleSheelPropetys) { Sheet sheet = multipleSheelPropety.getSheet() != null ? multipleSheelPropety.getSheet() : initSheet; if (!CollectionUtils.isEmpty(multipleSheelPropety.getData())) { sheet.setClazz(multipleSheelPropety.getData().get(0).getClass()); } writer.write(multipleSheelPropety.getData(), sheet); } } catch (FileNotFoundException e) { log.error("找不到文件或文件路徑錯誤, 文件:{}", filePath); } finally { try { if (writer != null) { writer.finish(); } if (outputStream != null) { outputStream.close(); } } catch (IOException e) { log.error("excel文件導出失敗, 失敗原因:{}", e); } } } /*********************匿名內部類開始,可以提取出去******************************/ @Data public static class MultipleSheelPropety { private List<? extends BaseRowModel> data; private Sheet sheet; } /** * 解析監聽器, * 每解析一行會回調invoke()方法。 * 整個excel解析結束會執行doAfterAllAnalysed()方法 * * @author: chenmingjian * @date: 19-4-3 14:11 */ @Getter @Setter public static class ExcelListener extends AnalysisEventListener { private List<Object> datas = new ArrayList<>(); /** * 逐行解析 * object : 當前行的數據 */ @Override public void invoke(Object object, AnalysisContext context) { //當前行 // context.getCurrentRowNum() if (object != null) { datas.add(object); } } /** * 解析完所有數據后會調用該方法 */ @Override public void doAfterAllAnalysed(AnalysisContext context) { //解析結束銷毀不用的資源 } } /************************匿名內部類結束,可以提取出去***************************/ }
CommonUtil:工具類
public class CommonUtil { /** * 生成32位編碼,不含橫線 * * @return uuid串 */ public static String getUUID() { String uuid = UUID.randomUUID().toString().trim().replaceAll("-", ""); return uuid.toUpperCase(); } /** * 得到當前日期格式化后的字符串,格式:yyyy-MM-dd(年-月-日) * @return 當前日期格式化后的字符串 */ public static String getTodayStr(){ return new SimpleDateFormat("yyyy-MM-dd").format(new Date()) ; } /** * 將對象轉化成json * * @param t * @return * @throws JsonProcessingException */ public static <T> String toJson(T t) throws JsonProcessingException { return OBJECT_MAPPER.get().writeValueAsString(t); } }
UserPojoRes:實體類
@Setter @Getter @ToString public class UserPojoRes extends BaseRowModel implements Serializable { private static final long serialVersionUID = -2145503717390503506L; /** * 主鍵 */ @ExcelProperty(value = "ID", index = 0) private String id; /** * 姓名 */ @ExcelProperty(value = "用戶名", index = 1) private String name; public UserPojoRes(String id, String name) { this.id = id; this.name = name; } public UserPojoRes(){ } }
驗證
模板下載
這里將模板文件放在resources
中
@GetMapping("/exportExcelTempalte") @ApiOperation(value = "下載導入模板") public void exportExcelTempalte(HttpServletResponse response) throws Exception { //Resource目錄中的文件 String filePath = "/excels/導入模板.xlsx"; ClassPathResource classPathResource = new ClassPathResource(filePath); Workbook workbook=WorkbookFactory.create(classPathResource.getInputStream()); ExcelUtil.downLoadExcel("導入模板.xlsx", response, workbook); }
Excel文件導入
@PostMapping("/importExcel") @ApiOperation(value = "Excel文件導入") public Response importExcel(HttpServletRequest request, MultipartFile file, HttpServletResponse response) throws Exception { List<Object> objects = ExcelUtil.readMoreThan1000RowBySheetFromInputStream(file.getInputStream(),null); List<UserPojoRes> list = new ArrayList<>(); for (Object o : objects) { UserPojoRes userPojoRes = new UserPojoRes(); List<String> stringList = (List<String>) o; userPojoRes.setId(stringList.get(0) != null ? stringList.get(0).toString() : ""); userPojoRes.setName(stringList.get(1) != null ? stringList.get(0).toString() : ""); list.add(userPojoRes); } String json = CommonUtil.toJson(list); return new Response(json); }
Excel文件導出
@Value("${project.tmp.files.path}") public String filesPath; @GetMapping("/exportExcel") @ApiOperation(value = "Excel文件導出") public void exportExcel(HttpServletResponse response) throws Exception { //創建臨時文件 String path = filesPath + CommonUtil.getUUID() + ".xlsx"; List<UserPojoRes> list = new ArrayList<>(); UserPojoRes userPojoRes = new UserPojoRes("009", "張三"); UserPojoRes userPojoRes1 = new UserPojoRes("009", "李四"); list.add(userPojoRes); list.add(userPojoRes1); ExcelUtil.writeWithTemplate(path, list); // 根據excel創建對象 Workbook workbook = WorkbookFactory.create(new FileInputStream(path)); String fileName = "用戶模塊" + CommonUtil.getTodayStr() + ".xlsx"; ExcelUtil.downLoadExcel(fileName, response, workbook); }
關于SpringBoot中EasyExcel如何實現Excel文件的導入導出就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。