2016-03-03 85 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