使用easyexcel-------导出excel表格

easy excel的使用:

https://www.jianshu.com/p/3a64ade57bf2

https://blog.csdn.net/weixin_41103806/article/details/91960397

https://blog.csdn.net/Java_Mrsun/article/details/85678028

异常情况:

jar包冲突:https://blog.csdn.net/qq_35620501/article/details/93752801

Ajax不能下载文件:https://blog.csdn.net/weixin_41981080/article/details/94033776

easy excel在浏览器端导出表格:

一,表格对应的实体对象:

使用easyexcel-------导出excel表格_第1张图片

二,获取到浏览器的输出流,然后利用exsyexcel将表格信息写入到输入流中:

/*
	 * 导出报表
	 */
	@RequestMapping("/downData")
	@ResponseBody
	public void downData(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
		PageBean pageBean = new PageBean();

		List slist = new ArrayList();
		pageBean.setSize(0);
		// 获得表格信息
		if (request.getParameter("username") == null && request.getParameter("time") == null) {// 不执行搜索
			slist = sReportService.getTestResult(pageBean);

		} else {// 获得条件查询的数据
			String username = "";
			String time = "";
			String begintime = "";
			String endtime = "";
			if (request.getParameter("username") != null && request.getParameter("username") != "") {
				username = new String(request.getParameter("username").getBytes("iso-8859-1"), "UTF-8");
			}
			if (request.getParameter("time") != null && request.getParameter("time") != "") {
				time = request.getParameter("time");// 2019-09-12 00:00:00 ~2019-10-18 00:00:00
				System.out.println(time);
				begintime = time.substring(0, time.indexOf("~"));
				endtime = time.substring(time.indexOf("~") + 1);
			}
			pageBean.setSname(username);
			pageBean.setName(time);
			pageBean.setBegintime(begintime);
			pageBean.setEndtime(endtime);
			slist = sReportService.getTestResultByExample(pageBean);
		}
		ServletOutputStream out = response.getOutputStream();//获取浏览器的输出流
		try {
			List items = new ArrayList();//对应的表格对象集合
			for (Test_Result tr : slist) {
				TableItem t = new TableItem();
				t.setUsername(tr.getUser().getUsername());
				String time = new SimpleDateFormat("yyyy-MM-dd").format(tr.getBegintime());
				t.setDate(time);
				t.setTime(tr.getTime());
				t.setWounded(tr.getWounded() + "/150");
				t.setRed(tr.getRed());
				t.setYellow(tr.getYellow());
				t.setGreen(tr.getGreen());
				t.setAssess(tr.getAssess());
				t.setScore(tr.getScore());
				// System.out.println(tr.getBegintime());
				items.add(t);
			}
			// 设置EXCEL名称
			String fileName = "学生考核信息 .xls";
			String filenameEncoder = "";
			// 设置编码
			filenameEncoder = URLEncoder.encode(fileName, "utf-8");
			filenameEncoder = filenameEncoder.replace("+", " ");// stringObject.replace(要替换的文本,替换成的文本)
			// System.out.println(filenameEncoder);
			// 浏览器设置
			response.setHeader("Content-disposition", "attachment;filename=" + filenameEncoder);
			response.setContentType("application/vnd.ms-excel");
			response.setCharacterEncoding("utf-8");
			// 下载EXCEL
			ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLS);// new ExcelWriter(out, ExcelTypeEnum.XLS,false)加false就是不要表头
			Sheet sheet1 = new Sheet(1, 0, TableItem.class);
			sheet1.setSheetName("sheet1");
			writer.write(items, sheet1);
			out.flush();
			writer.finish();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

 

你可能感兴趣的:(ssm,excel,easyexcel)