您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何使用easypoi導(dǎo)入excel,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
使用easypoi導(dǎo)入excel
easypoi導(dǎo)入excel需要引入依賴:
<!--easypoi 導(dǎo)出導(dǎo)入excel依賴--> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>3.2.0</version> </dependency>
首先我們準(zhǔn)備一個(gè)excel模板并帶有一些數(shù)據(jù)
第二步:準(zhǔn)備一個(gè)實(shí)體對象
package com.xash.quartzDemo.entity; import cn.afterturn.easypoi.excel.annotation.Excel; import cn.afterturn.easypoi.excel.annotation.ExcelTarget; import com.fasterxml.jackson.annotation.JsonFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; @ExcelTarget("Layer") public class Layer { private String layerId; @Excel(name = "法規(guī)名稱", isImportField = "true_st") private String layerName; @Excel(name = "法規(guī)描述", isImportField = "true_st") private String description; @Excel(name = "法規(guī)發(fā)布日期",importFormat = "yyyy-MM-dd") private String releaseTime; @Excel(name = "法規(guī)上傳日期",importFormat = "yyyy-MM-dd") private String recordTime; private String fileName; public String getLayerId() { return layerId; } public void setLayerId(String layerId) { this.layerId = layerId == null ? null : layerId.trim(); } public String getLayerName() { return layerName; } public void setLayerName(String layerName) { this.layerName = layerName == null ? null : layerName.trim(); } public String getDescription() { return description; } public void setDescription(String description) { this.description = description == null ? null : description.trim(); } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); public String getReleaseTime() { return releaseTime; } public void setReleaseTime(Date releaseTime) throws ParseException { this.releaseTime =sdf.format(releaseTime); } public String getRecordTime() { return recordTime; } public void setRecordTime(Date recordTime) throws ParseException { this.recordTime =sdf.format(recordTime); } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName == null ? null : fileName.trim(); } }
此處,日期格式對象excel到實(shí)體不能自動(dòng)按格式轉(zhuǎn)換,也不知道原因,所以手動(dòng)格式化了日期
第三步
創(chuàng)建導(dǎo)入工具類
package com.xash.quartzDemo.util; import java.io.File; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.springframework.web.multipart.MultipartFile; import cn.afterturn.easypoi.excel.ExcelImportUtil; import cn.afterturn.easypoi.excel.entity.ImportParams; public class Upload { public static String executeUpload1(String uploadDir,MultipartFile file,String fileName) throws Exception { //文件后綴名 String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); //上傳文件名 String filename = fileName + suffix; //服務(wù)器端保存的文件對象 File serverFile = new File(uploadDir + filename); //將上傳的文件寫入到服務(wù)器端文件內(nèi) file.transferTo(serverFile); return filename; } public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass){ if (StringUtils.isBlank(filePath)){ return null; } ImportParams params = new ImportParams(); params.setTitleRows(titleRows); params.setHeadRows(headerRows); List<T> list = null; try { list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params); }catch (Exception e) { e.printStackTrace(); } return list; } public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){ if (file == null){ return null; } ImportParams params = new ImportParams(); params.setTitleRows(titleRows); params.setHeadRows(headerRows); List<T> list = null; try { list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params); } catch (Exception e) { e.printStackTrace(); } return list; } }
本例使用 public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass)方法
OK,準(zhǔn)備工作做好后,就可以實(shí)現(xiàn)excel的導(dǎo)入工作了
準(zhǔn)備一個(gè)controller前端控制器,
package com.xash.quartzDemo.controller; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.xash.quartzDemo.entity.Layer; import com.xash.quartzDemo.util.Upload; import cn.afterturn.easypoi.excel.entity.ImportParams; @RestController public class Eecelmport { @RequestMapping("/excel") public List<Layer> excel() { String fliename="模板導(dǎo)入.xlsx"; List<Layer>list=null;; try { list=Upload.importExcel("C:/Users/gaofei/Desktop/"+fliename, 1, 3, Layer.class); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } }
前端訪問:
http://192.168.2.81:8082/excel,后端響應(yīng)并返回:
[{"layerId":null,"layerName":"hello","description":"hello","releaseTime":"2015-07-17","recordTime":"2015-09-17","fileName":null},{"layerId":null,"layerName":"world","description":"world","releaseTime":"2015-08-17","recordTime":"2015-10-17","fileName":null}]
關(guān)于如何使用easypoi導(dǎo)入excel就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。