2 princess  m princess__m 于 2016.04.28 11:05 提问

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

没有service方法,请问controller和jsp页面如何写,另外报表模板放在webapp/admin/excel下

1个回答

li953348869
li953348869   2016.04.28 11: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; 
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!