java 使用EasyExcel导出Excel

maven仓库:
	   <dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>easyexcel</artifactId>
			<version>1.1.2-beta5</version></dependency>
   		 </dependencies>

后台代码:   
/**
	 * 导出Excel
	 */
	@RequestMapping("/exportMsg")
	@ResponseBody
	public int exportMsg(@RequestParam("codeId") Long codeId ,HttpServletResponse response) {
		int r = 0;
		try {

			List exportAuthUsers  = authenBusinessService.findByExportInviteCode(codeId);
			OutputStream out = response.getOutputStream();
			response.setContentType("multipart/form-data");
			response.setCharacterEncoding("utf-8");
			String fileName= URLEncoder.encode("客户列表","utf-8");//设置导出格式
			response.setHeader("Content-disposition", "attachment;filename="+fileName+".xlsx");
			ExcelUtil.writer(out,"人员列表",exportAuthUsers, ExportAuthUser.class);
			out.flush();
			r=1;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return r;
	}

后台工具类:

 /**
     * 导出Excel
     * @param out Servlet 流
     * @param sheetName sheet名称
     * @param data 数据
     * @param clazz 对应数据Class
     */
    public static void writer(OutputStream out, String sheetName, List<? extends BaseRowModel> data,Class<? extends BaseRowModel> clazz){
        ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
        Sheet sheet1 = new Sheet(1, 0, clazz);
        sheet1.setSheetName(sheetName);
        sheet1.setAutoWidth(Boolean.TRUE);
        writer.write(data,sheet1);
        writer.finish();
    }


前台请求(不能使用ajax请求):
     $('#outExcel').click(function(){
            var  codeIds = $("#codeIds").val();
			window.location.href = '${ctx}/datas/exportMsg?codeId='+codeIds;
       }

 

转载于:https://my.oschina.net/u/3727427/blog/3055933

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值