jxl写入excel实现数据导出功能

@RequestMapping(params = "method=export", method = RequestMethod.GET)
    public void exportConfig(HttpServletRequest request, HttpServletResponse response,
            @RequestParam("dataId") String dataId, @RequestParam("group") String group,
            @RequestParam("querymethod") String querymethod ,ModelMap modelMap) {
       try{
           //根据传过来的dataId和group查出当前页面的配置
           Page<ConfigInfo> pageConfigInfo = new Page<ConfigInfo>();
           //判断是模糊查询还是查询,querymethod从前端传过来的志只能是findConfigInfoLike或者findConfigInfo,因此querymethod有Like就是模糊查询
           if(querymethod.indexOf("Like") > 0 )
               pageConfigInfo = this.configService.findConfigInfoLike(MIN_PAGE_NUM, MAX_PAGE_SIZE, group, dataId);
           else
               pageConfigInfo = this.configService.findConfigInfo(MIN_PAGE_NUM, MAX_PAGE_SIZE, group, dataId);
           List<ConfigInfo> listConfigInfo = new ArrayList<ConfigInfo>(); 
           if (null != pageConfigInfo)
               listConfigInfo = pageConfigInfo.getPageItems();
           
           //创建文件
           String tempfileName = new String(FILE_NAME.getBytes("GBK"), "ISO8859_1");
           response.reset();
           //设定输出文件头
           response.setHeader("Content-disposition", "attachment; filename="+tempfileName);
           response.setContentType("application/x-msdownload");
           //取得输出流
           OutputStream outputStream = response.getOutputStream();
           //创建文件
           WritableWorkbook writableWorkbook = Workbook.createWorkbook(outputStream);
           //创建工作表对象
           WritableSheet writableSheet = writableWorkbook.createSheet(DEFAULT_SHEET, 0);
           //写入表头
           writableSheet.addCell(new Label(0, 0, "dataId"));
           writableSheet.addCell(new Label(1, 0, "group"));
           writableSheet.addCell(new Label(2, 0, "content"));
           //写入数据
           if(listConfigInfo!=null && !listConfigInfo.isEmpty()){
               for(int i=0; i<listConfigInfo.size(); i++){
                   writableSheet.addCell(new Label(0, i+1, listConfigInfo.get(i).getDataId()));
                   writableSheet.addCell(new Label(1, i+1, listConfigInfo.get(i).getGroup()));
                   writableSheet.addCell(new Label(2, i+1, listConfigInfo.get(i).getContent()));
               }
           }
           writableWorkbook.write();
           // 关闭工作簿
           writableWorkbook.close();
           outputStream.close();
       }catch(Exception e)
       {
           log.error("导出配置出错"+e);
       }
    }

 

posted @ 2015-09-30 16:10  季樊  阅读(529)  评论(0编辑  收藏  举报