poi导出excel 之工具类(一)

实现poi导出excel表格,最方便的就是封装工具类,以便公用,下面直接贴代码,亲测可用。需求差异请适当修改源码!

工具类源码见文尾。。。。。。


代码测试

一、前端请求:



二、后台代码:

@RequestMapping(value = "/down")
	@ResponseBody
	public String exprotExcel(@CurrentUser User user, HttpServletResponse response, UserGroup userGroup) {

		ExcelUtil excelUtil = new ExcelUtil();
		OutputStream out = null;
		try {
			out = response.getOutputStream();// 取得输出流
			// out.flush();
			response.reset();// 清空输出流

			response.setHeader("Content-disposition",
					"attachment; filename=" + new String("用户表".getBytes("GB2312"), "8859_1") + ".xls");// 设定输出文件头
			response.setContentType("application/msexcel");// 定义输出类型

		} catch (IOException e) {
			e.printStackTrace();
		}
		String[] headers = { "用户组名称", "区域名称" };
		String[] columns = { "groupName", "areaName" };
		//准备数据list --根据实际项目
		RequestBody rqb = new RequestBody();
		rqb.setQryid("1");
		rqb.setQrycount("" + Integer.MAX_VALUE);
		rqb.setSerialNum(LSUtil.getLsNum());
		rqb.setOpMrg(user.getUsername());
		List qrylist = userGroupService.getUserGroupList(rqb).getQrylist();
		List uList = new ArrayList();
		for (Qrylist qList : qrylist) {
			UserGroup ug = new UserGroup();
			ug.setGroupName(qList.getGroupName());
			uList.add(ug);
		}
		try {
			excelUtil.exportExcel("用户组表", headers, columns, uList, out, "");

		} catch (Exception e1) {
			e1.printStackTrace();
		}
		try {
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

执行后效果图:

poi导出excel 之工具类(一)_第1张图片




源码下载请点击:工具类源码

你可能感兴趣的:(⊹●POI技术)