阿里 EasyExcel 使用及避坑


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

原本在項目中使用EasyPoi讀取excel,后來為了統一技術方案,改用阿里的EasyExcel。EasyExcel和EasyPoi有一定的相似之處。

EasyExcel和EasyPoi效率對比:

因為數據量少,從效率上看幾乎沒有差別,EasyExcel略勝一籌。

 

使用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模型

@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屬性指定字段排序。

注意:這里和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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM