controller层。
@RequestMapping("/downAgendaInfotoExcel.do")
public void downAgendaInfotoExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String queryPara = request.getParameter("queryPara");
String aiId = (String) request.getSession().getAttribute("activityId");
JSONUtils.getMorpherRegistry().registerMorpher(
new DateMorpherEx(new String[] { "yyyy-MM-dd" },
(Date) null));
String ctxPath = request.getSession().getServletContext().getRealPath("/") ;
try {
String fileName = agendaService.generateCodesExcel(ctxPath, Integer.parseInt(aiId));
response.sendRedirect("/cnscecms-admin/activities/"+aiId+"/"+fileName);
} catch ( Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
out.print("议程信息导出失败!");
out.flush();
out.close();
}
}
service层
@Override
public String generateCodesExcel(String ctxPath,Integer activityId) throws IOException, RowsExceededException, WriteException, BiffException {
// TODO Auto-generated method stub
List<Map<?,?>> agendaInfoList = agendaMapper.queryAgendaInfoByActivityId(activityId);
//activitycodeinfo.xls 为导出活动票务信息的模板文件
File template = new File(ctxPath+"agenda/activitycodeinfo.xls");
String path = ctxPath +"activities/"+activityId+"/";
String fileName = activityId+"_"+DateHandler.dateToStr(new Date(),"yyyyMMddHHmmss")+".xls";
File excelFile = new File(path +fileName);
if (!excelFile.exists()) {
excelFile.createNewFile();
}
// 打开导出活动票务信息的模板文件
WritableWorkbook book = Workbook.createWorkbook(excelFile,Workbook.getWorkbook(template));
// 生成名为"第一页”的工作表,参数0表示这是第一页
WritableSheet sheet = book.getSheet(0);//createSheet("第一页", 0);
WritableCellFormat wc = new WritableCellFormat();
// 设置边框线
wc.setBorder(Border.ALL, BorderLineStyle.THIN);
//设置自动换行
wc.setWrap(true);
String theme =null;
String beginTime = null;
int i=2;//根据模板的设置,从第三行输出票务信息
for(Map map :agendaInfoList){
if(i == 2)
theme = map.get("Theme") == null?"" : (String)map.get("Theme") ;
beginTime = map.get("actTime").toString();
//设置序号
// sheet.addCell(new Label(0, i, Integer.toString(i -1)));
sheet.addCell(new Label(0, i, map.get("StartTime").toString().substring(0, 5),wc));
sheet.addCell(new Label(1, i, map.get("EndTime").toString().substring(0, 5),wc));
sheet.addCell(new Label(2, i, map.get("Content").toString(),wc));
sheet.addCell(new Label(3, i, map.get("Speaker").toString(),wc));
sheet.addCell(new Label(4, i, map.get("Guest_introduction")== null?"":(String)map.get("Guest_introduction"),wc));
i++;
}
//将汇总信息显示在表格的第一行
sheet.addCell(new Label(0,0,theme+"论坛议程"+" "+"开始日期: "+beginTime.substring(0, 10) ));
// 写入数据并关闭文件
book.write();
book.close();
return fileName;
}
js的函数
var url = BASE_URL+"downAgendaInfotoExcel.do?queryPara=" + JSON.stringify(sy.serializeObject($('#agendaForm'))) ;
window.location.href = url;