依赖
net.sf.jxls
jxls-core
1.0.6
compile
@RequestMapping("dyQrcodefirstAllExcel")
@ResponseBody
public void dyQrcodefirstAllExcel(HttpServletRequest request, HttpServletResponse response) {
String templateFileName= request.getServletContext().getRealPath("/") + "/template/QrCode.xls"; // 获取项目下Excel模版
String destFileName="xxx.xls";
List<Map<String, Object>> deallist = new ArrayList<Map<String,Object>>();
String encoderContent= request.getScheme() +"://" + request.getServerName() + ":" +request.getServerPort()+request.getContextPath()+"/frontSgzs/indexfirst.jsp?qrcode_id=";
String sql="select CONCAT('"+encoderContent+"', qrcodefirst_id) as url from wkrj_bus_qrcodefirst where firstone_id=?";
//System.out.println(sql);
deallist = jdbcTemplate.queryForList(sql,firstone_id);
//System.out.println();
//deallist = this.infoSetService.downTrain(basicInfoId);
Map<String,Object> beans = new HashMap<String,Object>();
beans.put("list", deallist); // 将数据放入Map中
XLSTransformer transformer = new XLSTransformer(); // 创建XLSTransformer对象
InputStream in = null;
OutputStream out = null;
try{
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(destFileName.getBytes("gb2312"),"ISO8859-1")+"");
in=new BufferedInputStream(new FileInputStream(templateFileName));
Workbook workbook= transformer.transformXLS(in, beans);
out=response.getOutputStream();
//将内容写入输出流并把缓存的内容全部发出去
( workbook).write(out);
in.close();
out.close();
}
catch (Exception e){
} finally {
try {
in.close();
out.close();
} catch (IOException e) {
}
}
}
模板