【问题标题】:jqGrid Export To Excel in ASP.net 2.0jqGrid 在 ASP.net 2.0 中导出到 Excel
【发布时间】:2010-07-30 05:45:00
【问题描述】:

嘿,我没有使用 MVC 模式。我只使用 ASP.net 2.0 C#。我使用的是 jqGrid 3.6 版本。

我知道有一个属性 excelexport 需要设置为 true,我们必须添加一个自定义按钮,然后单击该按钮我必须调用 jqgrid.excelExport 方法。但我必须将复杂对象发送到页面我如何发送它?我不想将搜索参数作为查询字符串发送,因为我的搜索参数会太长。

【问题讨论】:

    标签: asp.net jqgrid


    【解决方案1】:

    您可以在 C# 代码中创建它:

    protected void btnExcel_Click(object sender, EventArgs e)
        {
    
            string codigo;
            codigo = hdnHtml.Value;
    
            StringBuilder sb = new StringBuilder();
    
            StringWriter sw = new StringWriter(sb);
    
            HtmlTextWriter htw = new HtmlTextWriter(sw);
    
            Page pagina = new Page();
    
            HtmlForm form = new HtmlForm();
    
            Response.Clear();
    
            Response.Buffer = true;
    
            Response.ContentType = "application/vnd.ms-excel";
    
            Response.AddHeader("Content-Disposition", "attachment;filename=Visor.xls");
    
            Response.Charset = "UTF-8";
    
            Response.ContentEncoding = Encoding.Default;
    
    
            pagina.EnableEventValidation = false;
    
            pagina.DesignerInitialize();
    
            pagina.Controls.Add(form);
    
            pagina.RenderControl(htw);
    
            Response.Write(codigo);
    
            Response.End();
    
    
    
        }    
    

    }

    在后面的代码中你可以重复这个。

    添加到页面属性:

    validateRequest="false"

     <asp:Button ID="btnExcel" runat="server" Text="Export" 
                        onclick="btnExcel_Click" /> //create a button
    
    <asp:HiddenField ID="hdnHtml" runat="server" /> // create a  hiddenField
    
    
    $('[id$=btnExcel]').hide(); // hide the button with jquery function
    
    
    function exportXls() {
    
        var tbody = $("#jQGrid_Visor").html();
        var thead = $(".ui-jqgrid-htable").html();
        var htmlTable = "<table>" + thead + tbody + "</table>";
        var strCopy = htmlTable;
        $('[id$=hdnHtml]').val(strCopy); // fill the hidden with the table content
        $('[id$=btnExcel]').click(); // click button hidden
    }
    

    在我的例子中,我有一个带有右键的菜单,可以触发事件 exportXls,如果你愿意,你可以使用 C# 离开而不隐藏按钮

    var eventsMenu = {
                bindings: {
                    'actualizar': function(rowid) {
                        //alert('Accion [Actualizar] del elemento ' + t.id);
                        jQuery('#jQGrid_Visor').setGridParam(rowid).trigger("reloadGrid");
                    },
                    **'exportar': function(rowid) {
                        //alert('Accion [Exportar] del elemento ' + rowid.id);
                        exportXls();**
                    },
                    'full': function(t) {
                        // alert('Accion [Full Screen] del elemento ' + t.id);
                        window.open("./frmBrwVisorMantencionFullScreen.aspx?cod_flota=" + $('[id$=hdnCodFlota]').val(), "Full_Screen", "scrollbars=NO,Resizable=NO,toolbar=no,location=no,directories=no,status=no,menubar=no,fullscreen=yes");
                    },
                    'paste': function(t) {
                        alert('Accion [Pegar] del elemento ' + t.id);
                    },
                    'delete': function(t) {
                        alert('Accion [Eliminar] del elemento ' + t.id);
                    }
                }
            };
    

    【讨论】:

    • 您也可以对表格进行 htmlencode,这样您就不必 validaterequest=false。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多