JAvaエクスポートexcelテーブルaliba easyexcelを使用

1987 ワード

開発ではテーブルをエクスポートする必要がある場合が多く,従来はpoiなどのjarを用いてインポートエクスポートが行われていた.最近、アリババのeasyexcelエクスポートがプロジェクトのアドレスを記録するのに非常に便利であることが分かった.
依存を先に追加

        
            com.alibaba
            easyexcel
            1.1.2-beta5
        


コントロール層
   public void  downloadPointsList(@RequestParam(defaultValue = "0",required = false) int type ,
                                    @RequestParam(defaultValue = "",required = false) String  begintime,
                                    @RequestParam(defaultValue = "",required = false) String  endtime
                                    , HttpServletResponse response) throws IOException {
        List downLoads = memberPointsService.downloadBytime(type,begintime,endtime);
        OutputStream out = response.getOutputStream();
        ExcelWriter writer =new ExcelWriter(out, ExcelTypeEnum.XLSX);
        Sheet sheet1 =new Sheet(1, 0, MemberPoints.class);
        sheet1.setSheetName("    ");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" +new String(( "      .xlsx").getBytes(), "ISO8859-1"));
        writer.write(downLoads, sheet1);
        writer.finish();
        out.flush();
        out.close();
    }




ここでアリは実体クラスの注釈に基づいてヘッダ対応を生成する.
public class MemberPoints  extends BaseRowModel {

    /**
     * ID
     */
     @ExcelProperty(value = "ID",index = 0)
    private Long id;

    /**
     *   ID
     */
    @ExcelProperty(value = "  ID",index = 1)
    private Long memberId;

    /**
     *    1   2   
     */
     @ExcelProperty(value = "  ",index =5)
    private Integer type;


}