没有service方法,请问controller和jsp页面如何写,另外报表模板放在webapp/admin/excel下
1条回答
- 是星星总会发光 2016-04-28 03:41关注
用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; }
解决 无用评论 打赏 举报
悬赏问题
- ¥15 数学建模招标中位数问题
- ¥15 phython路径名过长报错 不知道什么问题
- ¥15 深度学习中模型转换该怎么实现
- ¥15 HLs设计手写数字识别程序编译通不过
- ¥15 Stata外部命令安装问题求帮助!
- ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
- ¥15 TYPCE母转母,插入认方向
- ¥15 如何用python向钉钉机器人发送可以放大的图片?
- ¥15 matlab(相关搜索:紧聚焦)
- ¥15 基于51单片机的厨房煤气泄露检测报警系统设计