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); } } };
Felipe PΓ©rez Aug 19 '10 at 21:31 2010-08-19 21:31
source share