2011-02-24 50 views
1

我用這個AJAX調用控制器上我的Excel導出動作:Excel的結果沒有給我任何東西,但它運行良好

$("#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) { 

      } 
     }); 
    }); 

這裏是行動的樣子:

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運行良好,沒有錯誤,運行時代碼也不會出錯,但屏幕上沒有任何東西彈出。我期待看到一個保存對話框來保存Excel文件。

我在做什麼錯?

編輯:這裏是我的自定義操作,

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); 
     } 
} 

回答

2

您不能在ajax查詢上調用文件下載,因爲瀏覽器不會觸發文件下載彈出窗口。

而不是做一個AJAX調用控制器的方法,簡單地用一個

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

和唐「噸忘記添加你的論點。

希望這有助於

+0

謝謝!完美的作品。 – slandau 2011-02-24 16:13:39

0

我有我的導出到Excel中使用FileContentResult實現。它負責爲您設置響應標題。沒有必要重新發明輪子。 ;-)

不知道它是否會有用,但我可以推薦EPPlus庫來加載和處理Excel文件。

你可以嘗試將你的POST轉換爲GET並直接渲染標籤嗎? 我不認爲IE正在AJAX請求期間提取內容處置。

+0

我想但我不知道如何重寫它覆蓋它 – slandau 2011-02-24 15:51:19

+0

你不必重寫任何東西,只需從你的動作中返回新的FileContentResult(),它來自動作結果 – 2011-02-24 15:53:42

+0

我不是當然我明白你在說什麼,哈哈,對不起=/ – slandau 2011-02-24 15:56:52

相關問題