Excel中的结果没有给我任何东西,但它运行良好 [英] Excel Result not giving me anything, but it runs fine

查看:154
本文介绍了Excel中的结果没有给我任何东西,但它运行良好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个AJAX调用我的控制器上的Excel导出行动:

  $(#ExportToExcel)。点击(函数(){
        //调用Ajax做出口
        变种urlString =&其中;%= System.Web.VirtualPathUtility.ToAbsolute(〜/ MVC / Indications.cfc / ExportToExcel)%>中;
        VAR Jsondata =
                {
                    ID:GetGUIDValue()
                    的viewName:<%= VirtualPathUtility.ToAbsolute(〜/查看/指示/ TermSheetViews /交换/ CashFlows.aspx)%>中,
                    文件名:Cashflows.xls
                }
        $阿贾克斯({
            键入:POST,
            网址:urlString,
            数据:Jsondata,
            成功:功能(数据){

            }
        });
    });
 

下面是个什么动作看起来像:

 公众的ActionResult ExportToExcel(GUID?ID,字符串的viewName,字符串文件名)
        {
            IndicationBase指示= CachedTransactionManager< IndicationBase> .GetCachedTransactions(id.Value);
            返回新ExcelResult< Chatham.Web.Models.Indications.ModelBase>
            (
                ControllerContext,
                的viewName,
                文件名,
                indication.Model
            );
        }
 

萤火虫贯穿罚款,没有错误,或者和运行时,code没有错误,但没有弹出的屏幕上。我期待看到一个保存对话框,保存Excel文件。

我是什么做错了吗?

编辑:这是我的自定义操作,

 公共类ExcelResult<型号:GT; :的ActionResult
    {
        串_filename;
        串_viewPath;
        型号_model;
        ControllerContext _context;

        公共ExcelResult(ControllerContext背景下,字符串viewPath,字符串文件名,型号model)
        {
            this._context =背景;
            this._fileName =文件名;
            this._viewPath = viewPath;
            this._model =模型;
        }
         保护字符串RenderViewToString()
        {
            使用(VAR作家=新的StringWriter())
            {
                VAR视图=新WebFormView(_viewPath);
                VAR VDD =新的ViewDataDictionary<型号>(_模型);
                VAR viewCxt =​​新ViewContext(_context,查看,VDD,新TempDataDictionary(),作家);
                viewCxt.View.Render(viewCxt,作家);
                返回writer.ToString();
            }
        }
        无效WriteFile的(字符串内容)
        {
            HttpContext的背景下= HttpContext.Current;
            context.Response.Clear();
            context.Response.AddHeader(内容处置,附件;文件名=+ _filename);
            context.Response.Charset =;
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.ContentType =应用程序/ MS-Excel的;
            context.Response.Write(内容);
            context.Response.End();
        }

        公众覆盖无效的ExecuteReuslt(ControllerContext上下文)
        {
            串含量= this.RenderViewToString();
            this.WriteFile(内容);
        }
}
 

解决方案

您不能调用一个文件下载的AJAX查询,因为浏览器将不会触发文件下载弹出。

而不是做一个Ajax调用到控制器的方法,只需使用

  windows.open(yoururl / ExportToExcel ID = yourid和放大器;?等等......,NULL,NULL,NULL);
 

和唐吨忘记添加你的论点。

希望这有助于

I use this AJAX to call my Excel Export action on the controller:

$("#ExportToExcel").click(function () {
        // ajax call to do the export
        var urlString = "<%= System.Web.VirtualPathUtility.ToAbsolute("~/mvc/Indications.cfc/ExportToExcel")%>";
        var Jsondata =
                {
                    id: GetGUIDValue(),
                    viewName: "<%= VirtualPathUtility.ToAbsolute("~/Views/Indications/TermSheetViews/Swap/CashFlows.aspx")%>",
                    fileName: 'Cashflows.xls'
                }
        $.ajax({
            type: "POST",
            url: urlString,
            data: Jsondata,
            success: function (data) {

            }
        });
    });

Here is what the action looks like:

public ActionResult ExportToExcel(Guid? id, string viewName, string fileName)
        {
            IndicationBase indication = CachedTransactionManager<IndicationBase>.GetCachedTransactions(id.Value);
            return new ExcelResult<Chatham.Web.Models.Indications.ModelBase>
            (
                ControllerContext,
                viewName,
                fileName,
                indication.Model
            );
        }

Firebug runs through fine, no errors, and the code doesn't error when running either, but nothing pops up on the screen. I am expecting to see a save dialog box to save the excel file.

What am I doing wrong?

EDIT: Here is my custom action,

public class ExcelResult<Model> : ActionResult
    {
        string _fileName;
        string _viewPath;
        Model _model;
        ControllerContext _context;

        public ExcelResult(ControllerContext context, string viewPath, string fileName, Model model)
        {
            this._context = context;
            this._fileName = fileName;
            this._viewPath = viewPath;
            this._model = model;
        }
         protected string RenderViewToString()
        {
            using (var writer = new StringWriter())
            {
                var view = new WebFormView(_viewPath);
                var vdd = new ViewDataDictionary<Model>(_model);
                var viewCxt = new ViewContext(_context, view, vdd, new TempDataDictionary(), writer);
                viewCxt.View.Render(viewCxt, writer);
                return writer.ToString();
            }
        }
        void WriteFile(string content)
        {
            HttpContext context = HttpContext.Current;
            context.Response.Clear();
            context.Response.AddHeader("content-disposition", "attachment;filename=" + _fileName);
            context.Response.Charset = "";
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.ContentType = "application/ms-excel";
            context.Response.Write(content);
            context.Response.End();
        }

        public override void ExecuteResult(ControllerContext context)
        {
            string content = this.RenderViewToString();
            this.WriteFile(content);
        }
}

解决方案

You cannot call a file download on a ajax query because the browser will not trigger the file download popup.

Instead of doing an ajax call to your controller method, simply use a

windows.open("yoururl/ExportToExcel?id=yourid&etc...", null, null, null);

And don"t forget to add your arguments.

Hope this helps

这篇关于Excel中的结果没有给我任何东西,但它运行良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆