//模板导出
Workbook workbook = null;
OutputStream outstream = null;
try {
SimpleDateFormat df = new SimpleDateFormat(“yyyyMMddHHmmss”);//设置日期格式
String newsNo = df.format(new Date());
String fileName = “测试_” + newsNo;
String filePro = 1233 + fileName + “.xls”;
//dowlandUrl为配置文件中下载到电脑的路径
// excel文件流输出到浏览器,选择下载路径
File f = new File(request.getSession().getServletContext().getRealPath("/") + fileName);
List list = new ArrayList();
//TestEntity为模板下载的实体
list.add(new TestEntity(“sss”, “XXX”));
list.add(new TestEntity(“sss22”, “XXX”));
list.add(new TestEntity(“sssEE”, “XXX”));
list.add(new TestEntity(“sss33”, “XXX”));
//为实体赋值
workbook = ExcelExportUtil.exportExcel(new ExportParams(), TestEntity.class, list);
outstream = response.getOutputStream();
/**这段代码可以实现Excel以文件流的形式到浏览器中,浏览器左下角出现Excel的下载提示,
*new String(fileName.getBytes(),"iso-8859-1")可是实现文件名为中文,不是乱码 */
response.reset();
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes(), "iso-8859-1") + ".xls");
response.setContentType("application/x-download");
workbook.write(outstream);
outstream.close();
} catch (Exception e) {// 发生不可预知异常,在此截获异常信息,并返回客户操作不成功
return ResultJson.success("失败");
}
return ResultJson.success("成功");