鍍金池/ 問(wèn)答/Java  HTML/ ajax請(qǐng)求,生成Excel導(dǎo)出,每次運(yùn)行到圖中箭頭處,前端就報(bào)“導(dǎo)出失敗”的e

ajax請(qǐng)求,生成Excel導(dǎo)出,每次運(yùn)行到圖中箭頭處,前端就報(bào)“導(dǎo)出失敗”的error了?

圖片描述

$("#excel_export").live("click",function(){
    var excel_date_begin =$("#excel_date_begin").val();
    var excel_date_end =$("#excel_date_end").val();
    if(excel_date_begin==""||excel_date_end==""){
        alert("請(qǐng)選擇時(shí)間");
        return false;
    }
    $.ajax({
        url:'../app/contract/agency_excel_export',
        data:{
            excel_date_begin:excel_date_begin,
            excel_date_end:excel_date_end,
        },
        type:'POST',
        dataType:'json',
        success:function(data){
        },
        error:function(){
            alert("導(dǎo)出失敗")
        }
    })
})
public static void agency_excel_export(OutputStream os, List<Contract> contracts) {
          
       try {   
           //創(chuàng)建Excel工作薄   
           HSSFWorkbook book = new HSSFWorkbook();   
           //在Excel工作薄中建一張工作表   
           HSSFSheet sheet = book.createSheet("代理費(fèi)統(tǒng)計(jì)");
           //設(shè)置單元格格式(文本)   
           //HSSFCellStyle cellStyle = book.createCellStyle();   
           //第一行為標(biāo)題行   
           HSSFRow row = sheet.createRow(0);//創(chuàng)建第一行   
           HSSFCell cell0 = row.createCell(0);   
           HSSFCell cell1 = row.createCell(1);   
           HSSFCell cell2 = row.createCell(2);   
           HSSFCell cell3 = row.createCell(3);   
           HSSFCell cell4 = row.createCell(4);   
           HSSFCell cell5 = row.createCell(5);   
           HSSFCell cell6 = row.createCell(6);   
           HSSFCell cell7 = row.createCell(7);   

           //定義單元格為字符串類型   
           cell0.setCellType(HSSFCell.CELL_TYPE_STRING);   
           cell1.setCellType(HSSFCell.CELL_TYPE_STRING);   
           cell2.setCellType(HSSFCell.CELL_TYPE_STRING);   
           cell3.setCellType(HSSFCell.CELL_TYPE_STRING);   
           cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
           cell5.setCellType(HSSFCell.CELL_TYPE_STRING);   
           cell6.setCellType(HSSFCell.CELL_TYPE_STRING);   
           cell7.setCellType(HSSFCell.CELL_TYPE_STRING);   

           //在單元格中輸入數(shù)據(jù)   
           cell0.setCellValue("客戶名稱");   
           cell1.setCellValue("客戶地址");   
           cell2.setCellValue("聯(lián)系人");   
           cell3.setCellValue("電話");   
           cell4.setCellValue("手機(jī)"); 
           cell5.setCellValue("郵箱");   
           cell6.setCellValue("QQ");   
           cell7.setCellValue("合同類型");   

           //循環(huán)導(dǎo)出數(shù)據(jù)到excel中   
           for(int i = 0; i < contracts.size(); i++) {
               Contract contract = contracts.get(i);
               //創(chuàng)建第i行   
               HSSFRow rowi = sheet.createRow(i + 1);   
               //在第i行的相應(yīng)列中加入相應(yīng)的數(shù)據(jù)   
               rowi.createCell(0).setCellValue(1);
               rowi.createCell(1).setCellValue(1);
               rowi.createCell(2).setCellValue(1);
               rowi.createCell(3).setCellValue(1);
               rowi.createCell(4).setCellValue(1);
               rowi.createCell(5).setCellValue(1);
               rowi.createCell(6).setCellValue(1);
               rowi.createCell(7).setCellValue(1);

           }   
           //寫(xiě)入數(shù)據(jù)  把相應(yīng)的Excel 工作簿存盤(pán)   
           book.write(os);   
       } catch (IOException e) {   
           e.printStackTrace();   
       }   
   }  
}
回答
編輯回答
裸橙

你是要下載Excel,不能用ajax請(qǐng)求的,需要要用widow.open打開(kāi)新的鏈接,用get方式傳遞參數(shù),瀏覽器才能處理返回的Excel文件,ajax是沒(méi)辦法讓瀏覽器彈出保存Excel的對(duì)話框的

2017年12月3日 23:16
編輯回答
陌如玉

通常,在web前端需要下載文件,都是通過(guò)指定<a>標(biāo)簽的href屬性,訪問(wèn)服務(wù)器端url即可下載并保存文件到本地。
但是這種方式使用的是HTTP GET方法,參數(shù)只能通過(guò)URL參數(shù)方式傳遞,無(wú)法使用POST方式傳遞參數(shù)。
于是,想到使用ajax方式下載文件。
結(jié)果:ajax方式下載文件時(shí)無(wú)法觸發(fā)瀏覽器打開(kāi)保存文件對(duì)話框,也就無(wú)法將下載的文件保存到硬盤(pán)上!
原因:ajax方式請(qǐng)求的數(shù)據(jù)只能存放在javascipt內(nèi)存空間,可以通過(guò)javascript訪問(wèn),但是無(wú)法保存到硬盤(pán),因?yàn)閖avascript不能直接和硬盤(pán)交互,否則將是一個(gè)安全問(wèn)題。
解決:使用form表單模擬ajaxti提交,然后下載文件

function downloadFileByForm(url, excel_date_begin, excel_date_end){
    var form = $("<form></form>").attr("action", url).attr("method", "get");
        form.append($("<input></input>").attr("type", "hidden").attr("name", "excel_date_begin").attr("value", excel_date_begin)); 
        form.append($("<input></input>").attr("type", "hidden").attr("name", "excel_date_end").attr("value", excel_date_end));
        form.appendTo('body').submit().remove();
}

參考鏈接

2017年9月5日 13:53