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);
}
}
};
Regards from Chile