excel的导出

js:
           //导出Excel
        $('#button_export').on('click', function(e) {
            if($('#select_service').val() != 'all'){
                exportExcel();
            }else{
                alert('请选择一个服务!');
            }
        });
        
        var exportExcel = function(){
            
            var beginTime;
            var endTime;
            var url;
            
            beginTime = $('#reservationtime').val().split(daterange.separator)[0];
            endTime = $('#reservationtime').val().split(daterange.separator)[1];
            
           if (moment(endTime).isBefore(moment(), 'day')) {
              url = "svcLogHis/exportExcelInvokeDelay.do";
          }else{
              url = "svcLog/exportExcelInvokeDelay.do";
          }
             var param = {
                             svcName:$('#select_service').val(),
                             beginTime : beginTime,
                             endTime:endTime
                         };
             var strdata = '';
             for(var key in param){
                 strdata += '&' + key + '=' + param[key];
             }
             strdata = strdata.substring(1);
             window.location.href = url+'?'+strdata;
        };
        
Controller:
          @RequestMapping(value = "/exportExcelInvokeDelay.do", method = { RequestMethod.POST, RequestMethod.GET })
    public void exportExcelInvokeDelay(HttpServletResponse response,HttpServletRequest request,
                                     @RequestParam(value = "svcName", required = true) String svcName,
                                     @RequestParam(value = "beginTime", required = true) String beginTime,
                                     @RequestParam(value = "endTime", required = true) String endTime){
        long begin = 0l;
        long end = 0l;
        List<EsbServiceLog> data = null;
         try {
             begin = DateUtil.getTimeFormat().parse(beginTime).getTime();
             end = DateUtil.getTimeFormat().parse(endTime).getTime();
             
             data = serviceLogDao.findLogDelayByConditionInProcess1(svcName, begin, end, 0, 0);
             String path = request.getSession().getServletContext().getRealPath("/");
             XSSFWorkbook xb = serviceLogDao.exportExcelForInvokeDelayExport(path + "resources/template/InvokeDelay.xlsx", data);
             FileOutputStream fileOutputStream = new FileOutputStream(path + "resources/template/download-InvokeDelay-export-" + svcName + ".xlsx");
             xb.write(fileOutputStream);
             fileOutputStream.close();
             String p = path + "/resources/template/download-InvokeDelay-export-" + svcName + ".xlsx";
             File file = new File(p);
             if (file.exists()) {
                 InputStream inputStream = null;
                 inputStream = new BufferedInputStream(new java.io.FileInputStream(p));
                 String fileName = new String(("调用延迟信息").getBytes("gb2312"), "ISO8859-1") + ".xlsx";
                 response.setContentType("application/" + fileName);
                 response.setHeader("Content-Disposition", "attachment;" + "filename=" + fileName);
                 FileCopyUtils.copy(inputStream, response.getOutputStream());
             }
         } catch (IOException io) {
             io.printStackTrace();
             logger.error("文件生成错误 :{}");
         } catch (Exception e) {
             e.printStackTrace();
             logger.error("一些其他的错误 :{}");
         }
        
    }
    
    Dao 实现层:
             @Override
    public XSSFWorkbook exportExcelForInvokeDelayExport(String fileName,
            List<EsbServiceLog> data) {
        XSSFWorkbook xb = null;
        try {
            xb = new XSSFWorkbook(new FileInputStream(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        XSSFSheet sheet = xb.getSheetAt(0);
        int x = 1;
        for (EsbServiceLog cln : data) {
            XSSFRow row = sheet.createRow(x);
            // 服务名
            XSSFCell cell0 = row.createCell((short) 0);
            cell0.setCellValue(cln.getSvcName());
            // 服务别名
            XSSFCell cell1 = row.createCell((short) 1);
            cell1.setCellValue(cln.getSvcAlias());
            // 操作名
            XSSFCell cell2 = row.createCell((short) 2);
            cell2.setCellValue(cln.getOpName());
            // 时间段
            XSSFCell cell3 = row.createCell((short) 3);
            cell3.setCellValue(cln.getBeginTime());
            // 调用次数
            XSSFCell cell4 = row.createCell((short) 4);
            cell4.setCellValue(cln.getDelayTime());
            x++;
        }
        return xb;
    }
     

原文地址:https://www.cnblogs.com/yanduanduan/p/4441841.html