【问题标题】:jqgrid/mvc 3 - Export to excel and raise a file download dialog?jqgrid/mvc 3 - 导出到 excel 并引发文件下载对话框?
【发布时间】:2011-09-28 07:58:59
【问题描述】:

我已经阅读了很多解决方案,但我还没有找到可行的解决方案。
我的问题很简单,将数据导出到 excel 文件并弹出文件下载对话框。
但是文件下载对话框不显示。我可能是从 View 到 Controller 的调用方法错误,因为我调试到 ExportToExcel 函数并且没有错误
预先感谢

这是视图:

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

【问题讨论】:

  • 我实际上以提交表单结束了我的解决方案,这是出现下载对话框的唯一方法。到目前为止,我还没有找到其他解决方案

标签: jquery asp.net-mvc-3 jqgrid download


【解决方案1】:

我遇到了同样的问题,并设法通过如下更改视图来解决:

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

【讨论】:

  • 谢谢,亲爱的朋友们
【解决方案2】:

请修改如下代码

而不是返回 View() 而是返回 new EmptyResult();

还有 Response.ContentType="application/vnd.ms-excel"

如果您的代码不起作用,您可以使用NPOI 进行 Excel 文件操作

【讨论】:

  • 您好,谢谢您的回复。我已经像你的建议一样改变了,但仍然没有希望:(
  • 请尝试第二个建议NPOI
  • 如果您是使用 excel 2007 的用户,请查看 ClosedXML closedxml.codeplex.com
【解决方案3】:

根本不渲染视图。

而不是返回 View() 返回 File() 传递 byte[] 与文件内容。 更多信息在这里:http://msdn.microsoft.com/en-us/library/dd460208.aspx

【讨论】:

  • 感谢您的回复。但是我尝试过您的建议,但仍然无法正常工作:(
【解决方案4】:

我认为最好的方法是使用 FileResult

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

【讨论】:

    猜你喜欢
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    相关资源
    最近更新 更多