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,"下載出錯");
            }
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM