JAVA WEB 导出 Excel表格

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013008827/article/details/49427399
<pre name="code" class="java"> public ModelAndView InvoiceExcel(final String mes, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
		final int huanchong = 2048;
		Map<String, Object> beans = new HashMap<String, Object>();
		ProjectQuery query = new ProjectQuery();
		if (!Strings.isNullOrEmpty(mes)) {
			query = JsonUtil.json2Object(URLDecoder.decode(mes, "UTF-8"), ProjectQuery.class);
		}
		List<ProjectView> list = projectService.findAll();
		beans.put("list", list);
		String url = ExcelUtil.exportAndDownload(beans, "ProjectList", request, response);//ProjectList为模板名称
 
 
esponse.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("UTF-8");
		java.io.BufferedInputStream bis = null;
		java.io.BufferedOutputStream bos = null;
		try {
			File file = new File(url);
			if (!file.exists()) {
				file.mkdir();
			}
			if (file.exists()) {
				response.setContentType("application/x-msdownload;");
				response.setHeader("Content-disposition", "attachment; filename=" + new String(file.getName().getBytes("utf-8"), "ISO8859-1"));
				response.setHeader("Content-Length", String.valueOf(file.length()));
				bis = new BufferedInputStream(new FileInputStream(url));
				bos = new BufferedOutputStream(response.getOutputStream());
				byte[] buff = new byte[huanchong];
				int bytesRead;
				while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
					bos.write(buff, 0, bytesRead);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bis != null) {
				bis.close();
			}
			if (bos != null) {
				bos.close();
			}
		}
		return null;
	}
<img src="https://img-blog.csdn.net/20151026175600657?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
<span style="font-family: punctuation, 微软雅黑, Tohoma; font-size: 14.44444465637207px; line-height: 24.44444465637207px;">JQuery EasyUI 新开交流群 511830793</span>
欢迎大家前来与我一起学习,共同进步!


 
  

猜你喜欢

转载自blog.csdn.net/u013008827/article/details/49427399