【问题标题】:Download excel file on Client side using Webservice and jQuery Ajax使用 Webservice 和 jQuery Ajax 在客户端下载 excel 文件
【发布时间】:2016-03-03 06:11:33
【问题描述】:

我想在服务器端以 excel 格式导出数据,并在单击按钮时将该 excel 文件下载到客户端。我创建了一个 Web 服务方法并从 jQuery 进行了 ajax 调用。在网络服务中,我能够创建 excel 并将其存储到服务器端模块,但我无法在客户端下载文件。我不知道该怎么做?谁能帮我?怎么做。 我已经附上了我所做的代码。

//网络服务代码

[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;

});

【问题讨论】:

  • 你的服务的输出是什么?使用 console.log 检查
  • @SatyakiChatterjee 到目前为止,我的网络服务将 excel 文件存储在服务器上,然后将该 excel 的名称返回给客户端,它只是工作文件,但我想将该 excel 下载到客户端而不是存储到服务器。

标签: jquery asp.net ajax excel web-services


【解决方案1】:

我不知道如何使用 ajax 调用创建和下载 excel,但我可以建议您将 excel 保存在服务器文件系统的某个位置并使用 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);
 }

【讨论】:

猜你喜欢
  • 2022-10-12
  • 2011-02-06
  • 2013-11-02
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-30
  • 2014-12-08
相关资源
最近更新 更多