jquery - 使用jquery ajax调用导出到excel

标签 jquery asp.net-mvc-4 devexpress export-to-excel

我正在开发 MVC4 项目。我有 Devexpress 报告工具栏,其中有一个用于导出到 Excel 的自定义按钮,因为内置功能存在单元格合并问题。

无论如何,单击该自定义按钮..我想运行导出到Excel代码..但它的工作正常..我的意思是它返回正确的html但不要求提示保存文件/下载文件,可能是因为ajax 调用...

这是ajax调用的代码

function ReportToolbar_ItemClick(s, e) {
        debugger;
        if (e.item.name == 'btnCustomeExport') {
            //  $.post('@Url.Action("ExportToExcel", "Report")');

            $.ajax({
                url: "@Url.Action("ExportToExcel", "Report")",
                type: "POST",
                success: function (data, textStatus, jqXHR) {
                    //data: data from server 
                    alert('success');
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert('error');
                }
            });
        }
    }

和 Controller 代码:

public ActionResult ExportToExcel()
        {
            try
            {
                GridView GridView1 = new GridView();
                Response.ClearContent();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "EmployeesData.xls"));
                Response.ContentType = "application/ms-excel";

                StringWriter stringWriter = new StringWriter();
                HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

                GridView1.AllowPaging = false;
                GridView1.DataSource = ReportExecutor.GetShopReportExportData(DateTime.Now, DateTime.Now);
                GridView1.DataBind();

                //This will change the header background color
                GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");

                //This will apply style to gridview header cells
                for (int index = 0; index < GridView1.HeaderRow.Cells.Count; index++)
                {
                    GridView1.HeaderRow.Cells[index].Style.Add("background-color", "#d17250");
                }

                int index2 = 1;
                //This will apply style to alternate rows
                foreach (GridViewRow gridViewRow in GridView1.Rows)
                {
                    gridViewRow.BackColor = Color.White;
                    if (index2 <= GridView1.Rows.Count)
                    {
                        if (index2 % 2 != 0)
                        {
                            for (int index3 = 0; index3 < gridViewRow.Cells.Count; index3++)
                            {
                                gridViewRow.Cells[index3].Style.Add("background-color", "#eed0bb");
                            }
                        }
                    }
                    index2++;
                }

                GridView1.RenderControl(htmlTextWriter);

                Response.Write(stringWriter.ToString());
                Response.End();
                return Json(new { successCode = "1" });
            }
            catch (Exception e)
            {
                return Json(new { successCode = "0" });
            }
        }

如果我调试代码..我确实在stringWriter中得到结果,但仍然看不到保存/下载选项??

最佳答案

由于它没有出现,您正在发送任何数据而不是 ajax 尝试:

window.location= "@Url.Action("ExportToExcel", "Report")";

或者只使用<a>中的url标签href

关于jquery - 使用jquery ajax调用导出到excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20272515/

相关文章:

jquery - 将导航栏品牌和链接与 Bootstrap 4 中的页面内容对齐

javascript - 完整日历 - 如何在完整日历的月 View 中向日期添加前导零

c# - Devexpress TileView 数据源

c# - DataGridView 数据源不更新

javascript - 使用 bootstrap 创建轮播

javascript - Angular 文件上传暂时禁用拖放

c# - 当我输入 localhost/MyController/MyActionName/1 时,我在可选参数中得到一个空值,为什么?

.NET:User.IsInRole 无法在 Visual Studio 中工作

c# - 如何仅使用 Razor 在 MVC 中的网格/列内添加链接操作

javascript - DevExtreme DataGrid onRowDblClick