从Javascript调用ASP.NET MVC中控制器的ActionMethod,该方法将导致Excel工作表保存

从Javascript调用ASP.NET MVC中控制器的ActionMethod,该方法将导致Excel工作表保存,javascript,asp.net-mvc,excel,Javascript,Asp.net Mvc,Excel,我想调用返回Excel工作表的控制器的ActionMethod。 我知道我可以简单地将URL重定向到 这会奏效的。 但是我想在调用控制器之前弹出一个繁忙的微调器,然后关闭微调器。 同时,控制器的ActionMethod将生成Excel报告并返回它 ActionMethod如下所示: public ActionResult CreateReport() { try { // Opening the Excel template...

我想调用返回Excel工作表的控制器的ActionMethod。 我知道我可以简单地将URL重定向到 这会奏效的。 但是我想在调用控制器之前弹出一个繁忙的微调器,然后关闭微调器。 同时,控制器的ActionMethod将生成Excel报告并返回它

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");
        }
    }
$(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!
    });
});
我试过阿贾克斯,但运气不好

以下是我尝试的大致想法:

            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.隐藏旋转器

提前谢谢

查看它-并观看演示-

您可以使用以下内容:

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");
        }
    }
$(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!
    });
});

你没提到哪部分坏了。。不管怎样你的想法只是重定向到一个新的网址是最好的方式来处理这个问题