zoukankan      html  css  js  c++  java
  • poi 导出Excel --实战代码(根据模板导出,复制代码用)

    html代码

      <a class="btn btn-primary fa fa-export" href="javascript:void(0);" onclick="expExcel()"><span>导出excel</span></a>

    js代码

    function expExcel() {
            
            var ids=new Array();
            $("input:checkbox:checked").each(function(){
                //ids.push($(this).val());
                ids.push($(this).attr("id"));
            });
            if(ids.length==0){
                alert("请至少选中一条记录");
                return;
            }
             // alert(ids);
            /*    for(var id in ids){
               alert(id);
            } */
            if (confirm("导出选中页面数据?")) {
            
                var url = __ctx + '/platform/xxx/xxx/exportExcel.htm?myId='+ ids;
                var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
                if (userAgent.indexOf("compatible") > -1
                        && userAgent.indexOf("MSIE") > -1) {//判断是否IE浏览器
                    window.location.href(url);
                } else {
                    window.open(url, "导出报表");
                }
            }
        }

    java代码

    @RequestMapping({ "exportExcel" })
        public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String[] aryIds = RequestUtil.getStringAryByStr(request, "myId");
            QueryFilter queryFilter = getQuerFilter(request);
            String myIds = "";
    
            for (int i = 0; i < aryIds.length; i++) {
                String tmp = aryIds[i].replace("jqg_xxxxGrid_", "");
    
                if (i != aryIds.length - 1) {
                    myIds += "'" + tmp + "',";
                } else {
                    myIds += "'" + tmp + "'";
                }
            }
            queryFilter.addParamsFilter("whereSql", "MY_ID_ in (" + myIds + ")");
            queryFilter.addParamsFilter("orderBySql", "TBSJ ASC");
    
            List<Ggdjqyfgfc> query = ggdjqyfgfcManager.query(queryFilter);
    
            String dirPath = FileUtil.getClassesPath() + File.separator + "template" + File.separator + "exportMode" + File.separator;
            String fileName = "ggdjfgfc.xls";
            FileInputStream inStream = new FileInputStream(new File(dirPath + fileName));
    
            HSSFWorkbook wb = new HSSFWorkbook(inStream);
            HSSFSheet sheet = wb.getSheetAt(0);
    
            HSSFCell cell = null;
    
            for (int i = 0; i < query.size(); i++) {
                Ggdjqyfgfc e = query.get(i);
                HSSFRow row = sheet.getRow(i + 2);
                row = ExcelUtil.getNotNullRow(sheet, i + 2);
    
                cell = ExcelUtil.getNotNullCell(wb, row, 0);
                cell.setCellValue(sdf.format(e.getTbsj()));
    
                cell = ExcelUtil.getNotNullCell(wb, row, 1);
                cell.setCellValue(e.getGdj() + "");
    
                cell = ExcelUtil.getNotNullCell(wb, row, 2);
                cell.setCellValue(e.getRjdl() + "");
    
            }
    
            fileName = "xxxx表_" + ".xls";
    
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("GB2312"), "8859_1"));
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            OutputStream out = response.getOutputStream();
            wb.write(out);
            out.flush();
            out.close();
        }
  • 相关阅读:
    [你必须知道的.NET] 第四回:后来居上:class和struct
    [你必须知道的.NET]第十回:品味类型值类型与引用类型(下)-应用征途
    [你必须知道的.NET]第十一回:参数之惑传递的艺术(上)
    [你必须知道的.NET] 第一回:恩怨情仇:is和as
    [Anytao.History] 排名进入1000,未来值得努力
    [你必须知道的.NET] 第三回:历史纠葛:特性和属性
    [你必须知道的.NET] 第八回:品味类型值类型与引用类型(上)-内存有理
    [你必须知道的.NET] 第五回:深入浅出关键字把new说透
    [你必须知道的.NET]第十二回:参数之惑传递的艺术(下)
    .NET 3.5
  • 原文地址:https://www.cnblogs.com/rdchen/p/13949719.html
Copyright © 2011-2022 走看看