zoukankan      html  css  js  c++  java
  • 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);
           }
        }
  • 相关阅读:
    java中的String.format使用
    白话解析平安笔试题:多线程交替打印
    centos7 yum install redis
    CentOS7 linux下yum安装redis以及使用
    开源规则流引擎实践
    java中System.err.print和System.out.print区别
    drools -规则语法
    小明历险记:规则引擎drools教程一
    规则引擎drools封装
    C#中Encoding.Unicode与Encoding.UTF8的区别
  • 原文地址:https://www.cnblogs.com/pwenlee/p/4849483.html
Copyright © 2011-2022 走看看