2016-03-03 123 views
0

我想在服務器端導出excel中的數據並將該excel文件下載到客戶端,然後單擊按鈕。我創建了一個Web服務方法,並從jQuery進行ajax調用。在web服務中,我能夠創建excel並將其存儲到服務器端模塊,但我無法在客戶端下載該文件。我不知道該怎麼做?誰能幫我?怎麼做。 我附上了我所做的代碼。使用Webservice和jQuery下載客戶端上的excel文件Ajax

// web服務代碼

[WebMethod] 
    [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)] 
    public string ExportReport(int SelectedValue, string KeyValue, string DdlCltVal, string DdlLocVal, string DdlstfVal, int BtnID, DateTime StrDate, DateTime EndDate, int currentPage) 
    { 
     try 
     { 
      CommonFunction obj = new CommonFunction(); 
      DataTable dt = new DataTable(); 
      string rhead = ""; 
      if (SelectedValue != 0 && KeyValue == "0" && DdlCltVal == "0" && DdlLocVal == "0" && DdlstfVal == "0") 
      { 
       CourierReportController objCtr = new CourierReportController(); 
       dt = ListToDataTable.ToDataTable(objCtr.GetDailyReport(0, 10, SelectedValue)); 
       rhead = "Daily"; 
      } 
      StringBuilder sb = new StringBuilder(); 
      foreach (DataColumn column in dt.Columns) 
      { 
       sb.Append(column.ColumnName + "\t"); 
      } 
      sb.Append("\n"); 
      foreach (DataRow row in dt.Rows) 
      { 
       for (int i = 0; i < dt.Columns.Count; i++) 
       { 
        sb.Append(row[i].ToString() + "\t"); 
       } 
       sb.Append("\n"); 
      } 
      string strFilename = "CourierReport_" + rhead + ".xls"; 
      string strUploadPath = Server.MapPath("userexports").ToString(); 
      File.WriteAllText(strUploadPath + "\\" + strFilename, sb.ToString()); 
      return strFilename;   
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
    } 

//客戶端代碼

$('#btnExport').click(function (e) { 
var data= JSON2.stringify({ 
        SelectedValue: selectedValue, 
        KeyValue: KeyValue, 
        DdlCltVal: ddlCltVal, 
        DdlLocVal: ddlCltVal,      
        DdlstfVal: ddlstfVal, 
        BtnID:btnid,      
        StrDate: strDate, 
        EndDate: endDate, 
        currentPage: currentPage 
       }); 
      $.ajax({ 
        contentType: "application/json; charset=utf-8", 
        type: 'POST',      url:'http://localhost:8043/Modules/CourierReport/services/CourierReport.asmx/ExportReport', 
        dataType: 'json', 
        data:data, 
        success: function(result){ 
        alert (result.d); 
        //should i do any thing here? 
        }, 
        error: function(error){ 
        alert("error"); 
        } 
       }); 
        return false; 

});

+0

服務的輸出是什麼?使用console.log檢查 –

+0

@SatyakiChatterjee到現在爲止我的web服務將excel文件存儲在服務器上,然後將該excel的名稱返回給客戶端,它只是工作文件,但我想將該excel下載到客戶端而不是存儲到服務器。 – Bharat

回答

0

我不知道要創建和下載Excel中使用Ajax調用,但我可以推薦你保存在服務器的文件系統中出類拔萃的地方,並使用window.location

示例代碼會下載它(不能修改你的代碼由於時間限制,但我會很樂意以後做)

的jQuery:

$.ajax({ 
    contentType: "application/json; charset=utf-8", 
    type: 'POST',      
    url:'http://localhost:8043/Modules/CourierReport/services/CourierReport.asmx/ExportReport', 
    dataType: 'json', 
    data:data, 
    success: function(result){ 
       window.location("saved file path with come here");  
      }, 
    error: function(error){ 
       alert("error"); 
      } 
}); 

ASMX文件代碼:

[WebMethod] 
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)] 
public string ExportReport() 
{ 
    System.IO.StringWriter sw = new System.IO.StringWriter(); 
    System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw); 
    gvFiles.RenderControl(htw); 
    string renderedGridView = sw.ToString(); 
    System.IO.File.WriteAllText(@"Path on server to save file", renderedGridView); 
} 
+0

我給你的建議保存的文件路徑,但它會引發以下錯誤TypeError:window.location不是函數 \t window.location(「http:// localhost:8043/CourierReport/services/userexports /」+ cl ... – Bharat

+0

嘗試window.location.href =路徑 – Imad

+1

感謝他的工作。 – Bharat