阿里 EasyExcel 使用及避坑

github地址:https://github.com/alibaba/easyexceljava

本來在項目中使用EasyPoi讀取excel,後來爲了統一技術方案,改用阿里的EasyExcel。EasyExcel和EasyPoi有必定的類似之處。git

EasyExcel和EasyPoi效率對比:github

由於數據量少,從效率上看幾乎沒有差異,EasyExcel略勝一籌。json

 

使用maven的方式引用EasyExcel安全

https://mvnrepository.com/artifact/com.alibaba/easyexcel多線程

<!--easyexcel-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>1.1.2-beat1</version>
        </dependency>

使用Java模型的方式使用easyexcel架構

Java模型app

@Data public class TotalAmount extends BaseRowModel implements Serializable { private Integer id; @ExcelProperty(value ="類型",index = 0) private String type;//開支類型 信用卡等
 @ExcelProperty(value = "金額",index =1) private String sum; @ExcelProperty(value = "來源",index =2) private String name;//開支來源 如:**銀行信用卡
 @ExcelProperty(value = "日期",index =3) private String date; @ExcelProperty(value = "狀態",index =4) private Integer status; @ExcelProperty(value = "備註",index =5) private String descr; }

使用Java模型的方式須要繼承 BaseRowModel ,字段上使用 @ExcelProperty 註解,註解中 value 屬性指定字段名,index屬性指定字段排序。maven

注意:這裏和EasyExcel不一樣的是,目前能夠使用只指定index和同時指定index和value的方式來匹配excel文件,可是若是隻指定value,則沒法讀取。大數據

@RequestMapping("/importExce") @ResponseBody public JsonResponse importExcel(@RequestParam("excelFile") MultipartFile excelFile, String type) throws IOException { JsonResponse jsonResponse = new JsonResponse(); String sm="2019-02"; List<Object> dataList = null; dataList = EasyExcelFactory.read(excelFile.getInputStream(), new Sheet(3, 1, TotalAmount.class)); int scuess = 0; int error = 0; for (Object o : dataList) { if (o instanceof TotalAmount) { TotalAmount importEntity = (TotalAmount) o; try { } catch (Exception e) { error++; e.printStackTrace(); continue; } } } } /* @RequestMapping("/importExce") @ResponseBody public JsonResponse importExce(){ JsonResponse jsonResponse = new JsonResponse(); File excelFile = new File ("E:\\工做文檔\\部門架構201902(bug).xlsx"); String sm="2019-02"; InputStream inputStream = new FileInputStream(excelFile); List<Object> dataList = null; dataList = EasyExcelFactory.read(inputStream, new Sheet(3, 1, TotalAmount.class)); int scuess = 0; int error = 0; for (Object o : dataList) { if (o instanceof TotalAmount) { TotalAmount importEntity = (TotalAmount) o; try { } catch (Exception e) { error++; e.printStackTrace(); continue; } } } }*/

注意:在使用EasyExcel時容易出的幾個錯誤:

For input "" 類型錯誤,應該是double等類型的字段有非double類型的數據

java.lang.NumberFormatException: multiple points 多線程使用非線程安全類報錯,實際是在日期格式裏有並不是指定日期格式的數據,好比空格,好比指定 yyyy/mm/dd 但數據是 yyyy-mm-dd

使用easyexcel寫出excel:

使用Java模型方式,返回模型列表,帶入方法便可

/** * 導出Excel * * @param request * @param response * @param map * @throws IOException */ @RequestMapping("export.do") public void export(HttpServletRequest request, String type, HttpServletResponse response, @RequestParam Map<String, Object> map) throws IOException { ServletOutputStream out = null; try { out = response.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true); String filename; String fileName = null; try { filename = new Date().toLocaleString(); fileName = new String((filename).getBytes(), "UTF-8"); Sheet sheet2 = new Sheet(2, 3, ImportEntityEasyExcel.class, "sheet", null); List<ImportEntityEasyExcel> list = service.getData(map); response.setCharacterEncoding("utf-8"); response.setContentType("application/vnd.ms-excel"); response.setHeader("content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "utf-8")); out.flush(); } catch (Exception e) { e.printStackTrace(); } finally { writer.finish(); try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }

總結:

easyexcel還有一些並不完善,可是大數據量操做效率高於easypoi

相關文章
相關標籤/搜索