2010-07-30 99 views
0

嘿,我沒有使用MVC模式。我只使用ASP.net 2.0 C#。我正在使用jqGrid 3.6版本。jqGrid導出到Excel中的ASP.net 2.0

我知道有一個屬性excelexport需要設置爲true,我們必須添加一個自定義按鈕,並點擊該按鈕,我必須調用jqgrid.excelExport方法。但我必須發送複雜的對象到頁面我如何發送它?我不想發送搜索參數作爲查詢字符串,因爲我的搜索參數將會太長。

回答

4

你可以在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(); 



    }  

}

在後面的代碼,你可以重複這一點。

添加到頁面的propiertes:

validateRequest = 「假」

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

你也可以htmlencode表,所以你沒有驗證請求= false。 – 2011-06-09 04:59:58