jqGrid的导出到Excel在ASP.net 2.0 [英] jqGrid Export To Excel in ASP.net 2.0

查看:94
本文介绍了jqGrid的导出到Excel在ASP.net 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哎,我不使用MVC模式。我只使用ASP.net 2.0 C#。我使用的jqGrid 3.6版本。

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


解决方案

您可以在C#code创建此:

 保护无效btnExcel_Click(对象发件人,EventArgs的发送)
    {        串codigo;
        codigo = hdnHtml.Value;        StringBuilder的SB =新的StringBuilder();        StringWriter的SW =新的StringWriter(某人);        HtmlTextWriter的HTW =新的HtmlTextWriter(SW);        页面pagina =新页面();        的HtmlForm形式=新的HtmlForm();        Response.Clear();        将Response.Buffer =真;        Response.ContentType =应用程序/ vnd.ms-EXCEL;        Response.AddHeader(内容处置,附件;文件名= Visor.xls);        Response.Charset的=UTF-8;        Response.ContentEncoding = Encoding.Default;
        pagina.EnableEventValidation = FALSE;        pagina.DesignerInitialize();        pagina.Controls.Add(表);        pagina.RenderControl(HTW);        的Response.Write(codigo);        到Response.End();    }

}

在code后面可以重复这一点。

添加到页面propiertes:


  

validateRequest =false的


 < ASP:按钮的ID =btnExcel=服务器文本=导出
                    的onclick =btnExcel_Click/> //创建一个按钮< ASP:HiddenField ID =hdnHtml=服务器/> //创建一个hiddenField
$('[ID $ = btnExcel]')隐藏()。 //隐藏与jQuery的功能按钮
功能exportXls(){    VAR TBODY = $(#jQGrid_Visor)HTML()。
    VAR THEAD = $(UI-的jqGrid-htable)HTML()。
    VAR HTMLTABLE =<表> + THEAD + TBODY +< /表>,
    VAR strCopy = HTMLTABLE;
    $('[ID $ = hdnHtml]')VAL(strCopy)。 //填补了隐藏在表中的内容
    $('[ID $ = btnExcel]')点击()。 //点​​击按钮隐藏
}

在我来说,我有正确的按钮,如果你愿意,你可以用C#不要离开隐藏按钮触发事件exportXls,菜单

  VAR eventsMenu = {
            绑定:{
                actualizar':功能(ROWID){
                    //警报('ACCION [Actualizar]德尔ELEMENTO'+ t.id);
                    jQuery的('#jQGrid_Visor')setGridParam(ROWID).trigger(reloadGrid);
                },
                **'exportar':功能(ROWID){
                    //警报('ACCION [Exportar]德尔ELEMENTO'+ rowid.id);
                    exportXls(); **
                },
                '全':功能(T){
                    //警报('ACCION [全屏]德尔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\");
                },
                '粘贴':功能(T){
                    警报('ACCION [Pegar]德尔ELEMENTO'+ t.id);
                },
                删除:功能(T){
                    警报('ACCION [Eliminar]德尔ELEMENTO'+ t.id);
                }
            }
        };

hey i am not using MVC pattern. I am only using ASP.net 2.0 C#. I am using jqGrid 3.6 version.

I know that there is one property excelexport which need to be set true and we have to add one custom button and the on click of that button i have to call jqgrid.excelExport method. but i have to send on complex object to the page how do i send it? I dont want to send the search parameters as query string because my search parameters are going to be too long.

解决方案

You can create this in the C# code:

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



    }    

}

In the code behind you can repeat this.

Add to propiertes of page:

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
}

In my case I have a menu with the right button that fires the event exportXls, if you want you can leave with C# not hide the button

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

这篇关于jqGrid的导出到Excel在ASP.net 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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