javascript - 通过ajax方法调用函数时未下载Excel文件

标签 javascript jquery excel asp.net-mvc asp.net-mvc-4

情况

我正在开发一个应用程序,其中可以有一个包含 X 个项目的网格,并且每个项目都有一个打印按钮。单击此打印按钮可以调用 ajax 函数,该函数将网格项的 ID 传递给 Controller ​​。我根据该 ID 检索相关数据,然后将其下载到 Excel 文件中。 (具体项目的检索尚未完成)

到目前为止我所拥有的

到目前为止,我已经有了下载 Excel 文件的基本代码以及我的网格。

问题

我面临的问题是,如果我单击“打印”按钮...即使在我的 exporttoexcel 中有一个断点,也不会发生任何事情。函数向我显示该函数已输入,我可以单步执行它,尽管没有错误,但什么也没有发生。但是,我添加了调用相同函数的随机按钮,当我单击该按钮时,就会下载 Excel 文件。因此,我认为该问题与 aJax 有关。

代码

<input type="button" value="Test" onclick="location.href='@Url.Action("ExportToExcel", "Profile")'" />

这是下载文件的代码。这是我添加的一个简单按钮。

function ExportToExcel(id) {
    $.ajax({
        type: "POST",
        url: "@Url.Action("ExportToExcel", "Profile")",
        data: { "id": id },
        dataType: "json"

    });
}

这是我想要工作的功能,但它不起作用,而且我看不出我出了什么问题。

导出到 Excel 代码

public void ExportToExcelx()
{
    var products = new System.Data.DataTable("teste");
    products.Columns.Add("col1", typeof(int));
    products.Columns.Add("col2", typeof(string));

    products.Rows.Add(1, "product 1");
    products.Rows.Add(2, "product 2");
    products.Rows.Add(3, "product 3");
    products.Rows.Add(4, "product 4");
    products.Rows.Add(5, "product 5");
    products.Rows.Add(6, "product 6");
    products.Rows.Add(7, "product 7");


    var grid = new GridView();
    grid.DataSource = products;
    grid.DataBind();

    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
    Response.ContentType = "application/ms-excel";

    Response.Charset = "";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    grid.RenderControl(htw);

    //Response.Output.Write(sw.ToString());
    //Response.Flush();
    //Response.End();
    // =============


    //Open a memory stream that you can use to write back to the response
    byte[] byteArray = Encoding.ASCII.GetBytes(sw.ToString());
    MemoryStream s = new MemoryStream(byteArray);
    StreamReader sr = new StreamReader(s, Encoding.ASCII);

    //Write the stream back to the response
    Response.Write(sr.ReadToEnd());
    Response.End();



    //  return View("MyView");
}

理论

我相信该错误以某种方式与 aJax 相关,我也在 Controller 中创建按钮,如下所示。 "<button type='button' class='btn btn-warning' onclick='ExportToExcel(" + c.id + ");'>Print</button>",

location.href='@Url.Action有效,我想知道尝试重做我的动态按钮是否可以解决我的问题。

感谢任何可以提供的见解。

最佳答案

是的,你是对的,你有ajax问题,基本上,当你的第一个ajax调用返回成功时,你必须从ajax调用中再次调用 Controller 操作。将以下代码片段添加到您的 ajax 调用中。

success: function () {

    window.location = '@Url.Action("ExportExcel", "Profile")?id='+id;
}

并且您必须更改 Controller 方法才能返回文件,如下所示

public FileResult ExportToExcelx()
{
	...............
	
	byte[] byteArray = Encoding.ASCII.GetBytes(sw.ToString());
	return File(byteArray, System.Net.Mime.MediaTypeNames.Application.Octet, "FileName.xlsx");                       
}

关于javascript - 通过ajax方法调用函数时未下载Excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60200223/

相关文章:

javascript - 设置位置而不刷新网页

javascript - 我如何使用鼠标悬停禁用 js 功能

javascript - 如何使用 jquery 或 javascript 搜索表中的多列

excel - 比较Excel中的两个范围(不是线性比较)

excel - 将 FormulaArray 求值保存到实际数组

javascript - 我需要计算带有对象的数组中的列总和

javascript - onLoad 和 onunload 事件未触发

javascript - nodejs : Async.系列只执行第一个函数就停止了

javascript - 使用 jquery 从动态创建的表中删除一行

excel - 'Find' 命令的表达式