2016-02-13 67 views
0

我的Jquery Php ajax調用在alert()函數中顯示下面的結果,但我想將此輸出作爲ajax調用的excelsheet下載。我該怎麼做?如何將ajax調用輸出轉換爲excel下載文件

<?xml version="1.0" encoding="UTF-8"?> 
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> 
<Worksheet ss:Name="semonExport Received Reports - "> 
<Table> 
<Row> 
<Cell><Data ss:Type="String">semon RECEIVING REPORT</Data></Cell> 
</Row> 
<Row> 
<Cell><Data ss:Type="String"> </Data></Cell> 
</Row> 
<Row> 
<Cell><Data ss:Type="String">S/O</Data></Cell> 
<Cell><Data ss:Type="String">SHIPPER</Data></Cell> 
<Cell><Data ss:Type="String">NOTIFY PARTY</Data></Cell> 
<Cell><Data ss:Type="String">INVOICE NO</Data></Cell> 
<Cell><Data ss:Type="String">PO</Data></Cell> 
<Cell><Data ss:Type="String">NG ITEM</Data></Cell> 
<Cell><Data ss:Type="String">TOTAL LOT</Data></Cell> 
<Cell><Data ss:Type="String">LOT RECEIVED</Data></Cell> 
<Cell><Data ss:Type="String">PKGS</Data></Cell> 
<Cell><Data ss:Type="String">QTY</Data></Cell> 
<Cell><Data ss:Type="String">UNIT</Data></Cell> 
<Cell><Data ss:Type="String">CBM</Data></Cell> 
<Cell><Data ss:Type="String">Cweight</Data></Cell> 
<Cell><Data ss:Type="String">Weight unit</Data></Cell> 
<Cell><Data ss:Type="String">MSMT</Data></Cell> 
<Cell><Data ss:Type="String">DEST</Data></Cell> 
<Cell><Data ss:Type="String">STYLE</Data></Cell> 
<Cell><Data ss:Type="String">R/CGO</Data></Cell> 
<Cell><Data ss:Type="String">SB NO</Data></Cell> 
<Cell><Data ss:Type="String">SB Date</Data></Cell> 
<Cell><Data ss:Type="String">RECVDOC</Data></Cell> 
<Cell><Data ss:Type="String">TOTAL VALUE</Data></Cell> 
<Cell><Data ss:Type="String">EXP</Data></Cell> 
<Cell><Data ss:Type="String">EXP DATE</Data></Cell> 
<Cell><Data ss:Type="String">LOC</Data></Cell> 
<Cell><Data ss:Type="String">REMARKS</Data></Cell> 
</Row> 
<Row> 
<Cell><Data ss:Type="String">marks</Data></Cell> 
<Cell><Data ss:Type="String">Young One</Data></Cell> 
<Cell><Data ss:Type="String">marks brothers</Data></Cell> 
<Cell><Data ss:Type="String">1000</Data></Cell> 
<Cell><Data ss:Type="String">marks</Data></Cell> 
<Cell><Data ss:Type="String">nagroupbd</Data></Cell> 
<Cell><Data ss:Type="String">1280</Data></Cell> 
<Cell><Data ss:Type="String">200</Data></Cell> 
<Cell><Data ss:Type="String">100</Data></Cell> 
<Cell><Data ss:Type="String">100</Data></Cell> 
<Cell><Data ss:Type="String">CARTONS</Data></Cell> 
<Cell><Data ss:Type="String">583.2</Data></Cell> 
<Cell><Data ss:Type="String">100</Data></Cell> 
<Cell><Data ss:Type="String">Kgs</Data></Cell> 
<Cell><Data ss:Type="String">180 X 180 X 180</Data></Cell> 
<Cell><Data ss:Type="String">OKLAND</Data></Cell> 
<Cell><Data ss:Type="String">marks</Data></Cell> 
<Cell><Data ss:Type="String">Men's Product</Data></Cell> 
<Cell><Data ss:Type="String">C 12908</Data></Cell> 
<Cell><Data ss:Type="String">2016-02-02</Data></Cell> 
<Cell><Data ss:Type="String">2016-02-02</Data></Cell> 
<Cell><Data ss:Type="String">Max20</Data></Cell> 
<Cell><Data ss:Type="String">2016-02-02</Data></Cell> 
<Cell><Data ss:Type="String">ndsfjlsdajflka 

dfasdf</Data></Cell> 
</Row> 
</Table> 
</Worksheet> 
</Workbook> 

我的jQuery的AJAX調用下面是這種類型的代碼

$(document).on("click","button[name=excelreceivedreports]",function() { 
//alert("work"); 
    url = getURL()+'/ajax/get_excelExport_receivedreports.php'; 

    fdr = $('select[name=forwarder]').val(); 
    frdt = $('input[name=fromdate]').val(); 
    todt = $('input[name=todate]').val(); 
    var data = "";         
     $('.plus-loader').animate({ 
      'opacity': 1 
     },500); 
     $.post(url, {forwarder:fdr,fromdt:frdt,todate:todt },function(msg){ 

      $('.plus-loader').animate({ 
      'opacity': 0 
     },500); 
      alert(msg); 
     //$(".data-excel").html(msg);  
     }); 
}); 

你能告訴我怎麼可以把這個警告信息下載Excel文件。我希望通過ajax調用。提前致謝。

+0

我不確定這是可能與ajax。您必須在成功回調中向服務器發出另一個非Ajax請求,可能通過一次性隱藏表單來獲取它。 http://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax –

回答

0

您可以添加一個按鈕,它可以將輸出保存到excel文件中。但爲此,你的迴應必須格式化好。例如:

$("#btnExport").click(function (e) { 
    window.open('data:application/vnd.ms-excel,' + $('#dvData').html()); 
    e.preventDefault(); 
}); 

this例如,如果你不知道這件事。

+0

適用於Chrome和FireFox,但不適用於IE11 – CJLopez

相關問題