我想在服務器端導出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;
});
服務的輸出是什麼?使用console.log檢查 –
@SatyakiChatterjee到現在爲止我的web服務將excel文件存儲在服務器上,然後將該excel的名稱返回給客戶端,它只是工作文件,但我想將該excel下載到客戶端而不是存儲到服務器。 – Bharat