Export jqGrid to Excel in ASP.net 2.0

Hey, I do not use the MVC pattern. I use only ASP.net 2.0 C #. I am using jqGrid version 3.6.

I know that there is one excelexport property that must be set to true, and we need to add one custom button, and when I click this button I need to call the jqgrid.excelExport method. but I need to send a complex object to a page, how do I send it? I don’t want to send search parameters to the query string because my search parameters will be too long.

0
jqgrid
Jul 30 '10 at 5:45
source share
1 answer

You can create this in 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 page propers:

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 right-click menu that fires the exportXls event if you want you to be able to leave C # without hiding 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); } } }; 
+4
Aug 19 '10 at 21:31
source share



All Articles