调用ASP中控制器的ActionMethod.. NET MVC从Javascript,结果在Excel工作表保存
Call ActionMethod of a Controller in ASP.NET MVC from Javascript that results in Excel Worksheet saved
我想调用一个返回Excel工作表的控制器的ActionMethod。我知道我可以简单地将URL重定向到http://website/Excel/GenerateReport这是可行的。但是我想在调用控制器之前弹出一个繁忙的旋转器,然后关闭旋转器。同时,控制器的ActionMethod会生成Excel Report并返回。
ActionMethod是这样的:
public ActionResult CreateReport()
{
try
{
// Opening the Excel template...
var fs = new FileStream(Server.MapPath(@"~'Content'Excel'Template.xlsx"), FileMode.Open, FileAccess.Read);
// Getting the complete workbook...
var templateWorkbook = new XSSFWorkbook(fs);
// Getting the worksheet by its name...
//HSSFSheet sheet = templateWorkbook.GetSheet("Sheet1");
var sheet = templateWorkbook.GetSheet("Report");
// Getting the row... 0 is the first row.
//HSSFRow dataRow = sheet.GetRow(4);
var dataRow = sheet.GetRow(4);
dataRow.CreateCell(0, CellType.Numeric);
dataRow.GetCell(0).SetCellValue(11.11);
// Forcing formula recalculation...
sheet.ForceFormulaRecalculation = true;
var ms = new MemoryStream();
// Writing the workbook content to the FileStream...
templateWorkbook.Write(ms);
TempData["Message"] = "Excel report created successfully!";
// Sending the server processed data back to the user computer...
return File(ms.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Report.xlsx");
}
catch (Exception ex)
{
TempData["Message"] = "Oops! Something went wrong.";
return RedirectToAction("NPOI");
}
}
我试过ajax,但运气不好…
这是我尝试的一个总体思路:
showProgress();
$.ajax({
url: ajaxUrl,
type: "get",
data: {
tmoCode: $("#tmoDropDownList").val(),
clientCode: $("#clientDropDownList").val(),
productCode: $("#productDropDownList").val(),
startDateCode: $("#startDateDropDownList").val(),
endDateCode: $("#endDateDropDownList").val()
},
success: function (response, textStatus, jqXHR) {
alert("Success");
},
error: function (jqXHR, textStatus, errorThrown) {
alert("Error Creating Excel Report!");
},
// callback handler that will be called on completion
// which means, either on success or error
complete: function () {
hideProgress();
}
});
任何想法都是赞赏的!但请记住,我需要:1. 显示旋转器2. 运行报告并将其作为excel工作表返回3.隐藏旋转控件
提前感谢!
查看- https://github.com/johnculviner/jquery.fileDownload并查看演示- http://jqueryfiledownload.apphb.com/
你可以这样写:
$(function() {
$(document).on("click", "a.downloadFile", function() {
showProgress();
$.fileDownload($(this).prop('href'), {
successCallback: function(url) {
hideProgress();
},
failCallback: function(responseHtml, url) {
hideProgress();
}
});
return false; //this is critical to stop the click event which will trigger a normal file download!
});
});
相关文章:
- 奇怪的Javascript结果
- Javascript(jQuery)给了我奇怪的结果
- JSONP请求返回结果,但也触发error_callback
- 将HTML表格导出到excel时,无法将数据加载到excel文件中
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- Javascript,输出结果后页面不断刷新
- 这是使用html快照和谷歌获取的预期结果吗?SEO/SPA
- 将地理编码结果转换为php变量以发布到mysql数据库
- AngularJS/HTML/Bootstrap元素用于动态搜索结果
- 提交按钮,该按钮位于使用 Excel VBA 的 Javascript 中
- JQuery:向多个匹配结果添加换行符的最简单方法
- 当查询不在displayField中时,引导Ajax Typeahead不显示结果
- webpack代码拆分了handlerbs文件——结果是文件很大
- JavaScript循环无法正确计算/显示结果
- 相同的RegExp返回不同的结果-第一次是正确的结果,第二次是null
- Node.js使用Series函数(模式?)实现流控制时出现意外结果
- 试图将onChange函数作为道具传递给GrandChlidren,结果是TypeError:这是未定义的
- 调用ASP中控制器的ActionMethod.. NET MVC从Javascript,结果在Excel工作表保存
- 量角器测试结果在Excel表格中
- 两个问题,如何在html中创建调查结果页面?以及我如何保存用户输入从表格在excel