2011-09-28 57 views
1

我已經閱讀了很多解決方案,但我還沒有找到工作。
我的問題很簡單,將數據導出到excel文件並引發文件下載對話框。
但是文件下載對話框不顯示。我可以從查看到控制器調用的方法是錯誤的,因爲我調試到ExportToExcel功能,而且也沒有錯誤
預先感謝
jqgrid/mvc 3 - 導出爲ex​​cel並引發文件下載對話框?

這是查看:

<script type="text/javascript"> 
    $(document).ready(function() { 
     jQuery("#list").jqGrid({ 
      url: '/documents/List', 
      datatype: 'json', 
      mtype: 'GET', 
      colNames: ['ID', 'File Name', 'Description', 'File', 'Modified', 'File Type', 'Access'], 
      colModel: [ 
        { name: 'ID', index: 'id', width: 40, align: 'left', key: true, editable: false, editrules: { edithidden: false }, edittype: 'text' }, 
        { name: 'FileName', index: 'filename', width: 315, align: 'left', editable: true, edittype: 'text', editrules: { required: true }, formoptions: { elmsuffix: ' *'} }, 
        { name: 'Description', index: 'description', width: 210, align: 'left', editable: true, edittype: 'text', editrules: { required: true }, formoptions: { elmsuffix: ' *'} }, 
        { name: 'File', index: 'file', hidden: true, enctype: "multipart/form-data", method: "post", editable: true, edittype: 'file', editrules: { edithidden: true, required: true }, formoptions: { elmsuffix: ' *'} }, 
        { name: 'Modified', index: 'modified', width: 105, align: 'left', editable: false, edittype: 'text', editoptions: { size: 20, dataInit: function (el) { $(el).datepicker({ dateFormat: 'mm/dd/yy' }); } } }, 
        { name: 'FileType', index: 'filetype', width: 210, align: 'left', editable: true, edittype: 'select', editrules: { required: true }, formoptions: { elmsuffix: ' *' }, 
         editoptions: { dataUrl: '/HtmlSelectHelper/ConstructDocumentTypeList' } 
        }, 
        { name: 'Access', index: 'access', width: 114, align: 'left', editable: true, edittype: 'select', editrules: { required: true }, formoptions: { elmsuffix: ' *' }, 
         editoptions: { value: '0:Private;1:Public' } 
        }, 
       ], 
      autowidth: false, 
      forceFit: false, 
      shrinkToFit: false, 
      width: 1024, 
      height: 600, 
      rowNum: 10, 
      rowList: [5, 10, 20, 50, 100], 
      pager: jQuery('#pager'), 
      sortorder: "desc", 
      sortable: true, 
      viewrecords: true, 
      caption: "Documents List", 
      editurl: "/documents/edit" 
     }); 
     jQuery("#list").jqGrid('navGrid', '#pager', 
     { 
      add: true, edit: true, view: true, del: true 
     }, 
     { 
      closeAfterEdit: true, 
      closeAfterAdd: true, 
      width: 400 
     }, 
     { 
      closeAfterEdit: true, 
      closeAfterAdd: true, 
      width: 400, 
      serializeEditData: function (data) { return $.param($.extend({}, data, { id: 0 })); } 
     }, 
     { 
    }, 
     { 
      multipleSearch: true 
     }); 

    jQuery("#list").jqGrid('navButtonAdd', '#pager', { caption: "", buttonicon: "ui-icon-calculator", title: "choose columns", 
     onClickButton: function() { 
      jQuery("#list").jqGrid('columnChooser'); 
     } 
    }); 

    jQuery("#list").jqGrid('navButtonAdd', '#pager', { 
     caption: "", buttonicon: "ui-icon-print", title: "Excel Export", 
     onClickButton: function() { 
         $.post("/Documents/ExportToExcel", {}, function() { 

         }); 
     } 
    }); 

    jQuery("#list").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: true, defaultSearch: "cn" }); 
});    
</script> 

這是控制器:

public ActionResult ExportToExcel() 
{ 
    var documents = db.documents as IEnumerable<document>; 

    var grid = new GridView 
        { 
         DataSource = from document in documents 
            select new 
               { 
                filename = document.filename, 
                description = document.description, 
                modified = document.modified.ToString(), 
                filetype = document.filetype, 
                access = document.access 
               } 
        }; 

    grid.DataBind(); 

    Response.ClearContent(); 
    Response.AddHeader("content-disposition", "inline; filename=Excel.xls"); 

    Response.ContentType = "application/excel"; 

    StringWriter sw = new StringWriter(); 

    HtmlTextWriter htw = new HtmlTextWriter(sw); 

    grid.RenderControl(htw); 

    Response.Write(sw.ToString()); 

    Response.End(); 
    return View("Index"); 
} 
+0

我實際上通過提交表單結束了我的解決方案,這是下載對話框的唯一方法。我不知道到目前爲止的另一個解決方案 – Dranix

回答

0

請更改代碼如下

,而不是返回查看()返回新EmptyResult()的;

也Response.ContentType =「應用程序/ vnd.ms-EXCEL」

如果您的代碼不工作,你可以使用NPOI爲Excel文件操作

+0

嗨,thx你的迴應。我已經改變了你的建議,但它仍然沒有希望:( – Dranix

+0

請嘗試第二個建議NPOI – Prasanth

+0

,如果你是用戶使用Excel 2007檢查out ClosedXML http://closedxml.codeplex.com/ – bbqchickenrobot

0

我認爲最好的方法是使用FileResult

public FileResult Binary(MyModel Model) 
    { 
     return new FileContentResult(bindata, "application/vnd.ms-excel") 
        { 
         FileDownloadName = "mytestfile.xls" 
        };   
    } 
1

我有同樣的問題,並設法通過改變視圖來解決如下:

jQuery("#list").jqGrid('navButtonAdd', '#pager', { 
    caption: "", buttonicon: "ui-icon-print", title: "Excel Export", 
    onClickButton: function() { 
        window.location.href = "/Documents/ExportToExcel"; 
    } 
}); 
+0

謝謝,親愛的朋友 –