JAVA web端JS下载excel文件

JSP代码如下:

JSP端引入jquery.easyui.min.js库:

<script type="text/javascript" src="<c:url value="/resources/jquery/jQuery-2.2.0.min.js"/>"></script> <!--先引入jquery库-->

<script type="text/javascript" src="<c:url value="/resources/plugins/jquery.easyui.min.js"/>"></script> <!-- 再引入jquery.easyui.min.js库 -->

<!-- 隐藏form表单,下载excel使用 -->
<form id="excel" action='' method="get">
        <input type="hidden" id="id" name="id" />
        <input type="hidden" id="startTime" name="startTime" />
        <input type="hidden" id="endTime" name="endTime" />
</form>

 

JS代码如下:

function downloadExcel(id){
    console.log("id",id);
    $("#excel").find("#startTime").val(startTime.val());
    $("#excel").find("#endTime").val(endTime.val());
    $("#excel").find("#id").val(id);
//下载excel表单提交 $(
'#excel').form('submit', { url : basePath + "device/downloadExcel", success : function(data) { var msg = eval('(' + data + ')').msg; if(msg!=null){ showMsgModal(msg); } } }); }

 

springMVC后台代码如下:

@RequestMapping(value = "/downloadExcel", method = RequestMethod.GET)
    public void excel(HttpServletRequest request,HttpServletResponse response,HttpSession session,
            @RequestParam(value = "id", required = false) String id,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime){
        OutputStream responseOutput = null;
        try{
            if(StringUtils.isBlank(id)){
                write2response(response,"ID号不能为空");
                return;
            }
            if(StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)){
                write2response(response,"开始时间或结束时间不能为空");
                return;
            }
            XSSFWorkbook wbook = new XSSFWorkbook();  //测试下载用,所以这里excel内容直接写死
            XSSFSheet sheet = wbook.createSheet("sheet000");  
            XSSFRow row = sheet.createRow((int) 0);  
            Cell cell = row.createCell(0);
            cell.setCellValue("abc123");
            //写excel文件到response输出流
            //下载文件名称
            String fileName = id+"["+startTime+"-"+endTime+"]" + DateUtil.yyyy_MM_dd_HH_mm_ss();
            fileName = new String(fileName.getBytes("GBK"), "iso8859-1");
            response.reset();// 清空输出流
            response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
            response.setContentType("application/msexcel");// 定义输出类型
            responseOutput = response.getOutputStream();
            wbook.write(responseOutput);
            responseOutput.flush();
        } catch (IOException e1) {
            logger.error(e1);
            write2response(response,"下载出错");
        } finally{
            try {
                if(null != responseOutput){
                    responseOutput.close();
                }
            } catch (IOException e) {
                logger.error(e);
                write2response(response,"下载出错");
            }
        }
    }

 

posted on 2017-09-07 10:39  梦幻朵颜  阅读(8997)  评论(0编辑  收藏  举报