调用ASP中控制器的ActionMethod.. NET MVC从Javascript,结果在Excel工作表保存

Call ActionMethod of a Controller in ASP.NET MVC from Javascript that results in Excel Worksheet saved

本文关键字:结果 Excel 工作 保存 Javascript 控制器 ASP ActionMethod MVC NET 调用      更新时间:2023-09-26

我想调用一个返回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!
    });
});