java excel模板导出数据

这么做的导出可以按照事先准备的excel格式与央视进行导出,看着会舒心一点。并无其他用处。

jsp页面(jeecg框架)

    <t:dgToolBar title="政审导出" icon="icon-download" url="" funname="exportFile('${fid}','excel')" ></t:dgToolBar>

js方法
    function exportFile(dqjcid,type){
        var plansize = $('#exhibitorList').datagrid('getRows').length;
        if(plansize == 0){
            tip('无数据!');
            return;
        }
         var jcid =  $("select[name='forthetimeid'] option:selected").val(); 
         var jcids = "";
         if(jcid){
             jcids = "&jcid="+jcid;
         }else{
             jcids = "&jcid="+dqjcid;
         }
             var params = "";
             var url = "";
             if(type == 'excel'){
                 url = "activityplanController.do?exportXlsZS";
             }
              var ids = [];
              var rows = $("#exhibitorList").datagrid('getSelections');
              if (rows.length > 0) {
                  for ( var i = 0; i < rows.length; i++) {
                        ids.push(rows[i].id);
                    }
                  ids = ids.join(',');
                  params = "&ids="+ids; 
                  window.open(url+encodeURI(params),false);
              }else{
                  tip('请至少选择一条数据!');
              }
    }

java 后端代码

    @RequestMapping(params = "exportXlsZS")
    public void exportXlsZS(HttpServletRequest request,
            HttpServletResponse response) throws IOException, InvalidFormatException {
        String ids = request.getParameter("ids");
        if(StringUtils.isNotEmpty(ids)) {
            if(ids.contains(",")){
                ids = ids.replace(",", "','");  
            }
        }
        Session session = sessionFactory.openSession();
        String sql = "select A.PK_INFORMATION as id, rownum as num,A.SURNAME_CN as surname,A.GIVENNAME_CN as givenname,"
                + "A.SURNAME_EN as surnameen, A.GIVENNAME_EN as givennameen,A.POSITION as posi,"
                + "A.SEX as sex,A.IDTYPE as idtype,A.IDNUMBER as idnumber,A.WORKPHONE as phone ,"
                + "B.NAME_CN AS company from GH_BFM.bfm_fm_participant A,GH_BFM.BFM_FM_EXHIBITOR B "
                + "WHERE A.REFID=B.PK_INFORMATION and A.REFID IN ('"+ids+"')";
        SQLQuery query = session.createSQLQuery(sql.toString()).addEntity(ParinfoVo.class);
        List<ParinfoVo> lists = query.list();
            List<DictEntity> sexs = systemService.findByProperty(DictEntity.class, "dictname", "sex"); //性别字典
            List<DictEntity> idcadtypes = systemService.findByProperty(DictEntity.class, "dictname", "iDTypeDict");//证件类型字典
        
        for (ParinfoVo person : lists) {
            for (DictEntity dictEntity : sexs) {
                    if(person.getSex().equals(dictEntity.getDictkey())) {
                        person.setSex(dictEntity.getDictvaluecn());
                        }
            }
            for (DictEntity dictEntity : idcadtypes) {
                if(person.getIdtype().equals(dictEntity.getDictkey())) {
                    person.setIdtype(dictEntity.getDictvaluecn());
                    }
            }
        }
            String templetePath = "/export/template/import_userFilmMarket.xls"; //excel模板地址
            Map<String, Object> beans = new HashMap<String, Object>();
            beans.put("result", lists);
            XLSTransformer transfer = new XLSTransformer();
            URL url = getClass().getResource(templetePath);
            File file = new File(url.getPath());
            FileInputStream fileInputStream = new FileInputStream(file);
            org.apache.poi.ss.usermodel.Workbook workbook = transfer.transformXLS(fileInputStream, beans);
         PropertiesUtil util = new PropertiesUtil("sysConfig.properties"); 
        String strExcelPath = util.readProperty("exportExcelPath");//获取文件导出地址
        String dateStr =DateUtils.getDate("yyyyMMddHHmmss") ;
        File excelPath = new File(strExcelPath);
        if(!excelPath.exists()) {
            excelPath.mkdir();
        }
        FileOutputStream fos = new FileOutputStream("import_userFilmMarket.xls"); 
        workbook.write(fos);
          String codedFileName = "systemdata";
        response.setHeader("Content-Disposition",
                "attachment;filename=" + "import_userFilmMarket" + ".xls");
        // 响应类型,编码
        response.setContentType("application/octet-stream;charset=UTF-8");
        // 形成输出流
        OutputStream osOut = response.getOutputStream();
        // 将指定的字节写入此输出流
        workbook.write(osOut);
        // 刷新此输出流并强制将所有缓冲的输出字节被写出
        osOut.flush();
        // 关闭流
        fileInputStream.close();
        response.getOutputStream().close();
        fos.close();
        osOut.close();
        
        File excelFiles = new File(strExcelPath+"/"+dateStr+".xls");
        if(excelFiles.exists()) {
            excelFiles.delete();
   }
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值