诶诶诶略 2016-04-28 03:05 采纳率: 0%
浏览 3501

spring mvc实现导出数据到excel,页面上有数据,只在controller里面写

没有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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题