没有service方法,请问controller和jsp页面如何写,另外报表模板放在webapp/admin/excel下
用poi
/**
* 导出exec
* @param request
* @param response
* @param model
* @return
* @throws UnsupportedEncodingException
*/
@RequestMapping(value = "importExec", method = RequestMethod.GET)
@ResponseBody
public String importExec(HttpServletRequest request, HttpServletResponse response, Model model) throws UnsupportedEncodingException {
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
HSSFFont font = wb.createFont();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("前端订单");
/* for (int i = 0; i < 11; i++) {
sheet.setColumnWidth(i, 100*60);
}*/
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("订单号");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("服务月嫂");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("服务城市");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("下单时间");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("服务时间");
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setCellValue("结束时间");
cell.setCellStyle(style);
cell = row.createCell((short) 6);
cell.setCellValue("订单总价");
cell.setCellStyle(style);
cell = row.createCell((short)7);
cell.setCellValue("订单状态");
cell.setCellStyle(style);
Map<String,Object> paramMap = new HashMap<String,Object>();
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
List<OrderTrade> tradeList =this.orderTradeService.selectAll();
OrderTrade ot=null;
for (int i = 0; i < tradeList.size(); i++)
{
row = sheet.createRow((int) i + 1);
ot=new OrderTrade();
ot=tradeList.get(i);
// 第四步,创建单元格,并设置值
row.createCell((short) 0).setCellValue(ot.getOrderCode());
row.createCell((short) 1).setCellValue(ot.getMatronName());
row.createCell((short) 2).setCellValue(ot.getCityName());
row.createCell((short) 3).setCellValue(ot.getOrderCreateDate());
row.createCell((short) 4).setCellValue(ot.getServiceStartDate());
row.createCell((short) 5).setCellValue(ot.getActualEndDate());
row.createCell((short) 6).setCellValue(ot.getGrandTotal());
row.createCell((short) 7).setCellValue(ot.getOrderStatusName());
}
// 第六步,将文件存到指定位置
try
{
Map<String, Object> retMap = new HashMap<String, Object>();
String resultStr = null;
String filePath = request.getSession().getServletContext().getRealPath("/")+"/TradeOrder.xls";
File file=new File(filePath);
//File file=new File("D:/TradeOrder.xls");
if(!file.exists())
{
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream fout = new FileOutputStream(file);
wb.write(fout);
fout.close();
retMap.put("result", true);
resultStr = JsonUtils.toJsonString(retMap);
return resultStr;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}