您的位置:首页 > 编程语言 > Java开发

JAVA WEB 导出 Excel表格

2015-10-26 17:33 573 查看
<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>
欢迎大家前来与我一起学习,共同进步!



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java java web excel