簡體   English   中英

使用Webservice和jQuery Ajax在客戶端下載excel文件

[英]Download excel file on Client side using Webservice and jQuery Ajax

我想在服務器端的excel中導出數據,然后單擊按鈕將該excel文件下載到客戶端。 我創建了一個Web服務方法,並從jQuery進行了ajax調用。 在Web服務中,我能夠創建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;

});

我不知道使用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);
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM