怎么实现jqGrid导出excel   
$("#AttentionList").jqGrid('navButtonAdd','#AttentionListPager',{
position:'last',title:'导出全部',caption:'',buttonicon:"ui-icon-disk",onClickButton:exportExcel
   });  
function exportExcel(){
                   
        alert("正在导出为Excel文件......请稍等");
        //这里怎么实现导出的方法?    }

解决方案 »

  1.   


    将grid的数据源通过后台代码导出excel即可。<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";response.setHeader("Content-disposition", "attachment; filename=file.xls");//就这句,它就是excel
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'excel.jsp' starting page</title>
        
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">  </head>
      
      <body>
        <table>
         //在这个表格中显示jqGrid的数据后,它就是一个excel了
        </table>
      </body>
    </html>
      

  2.   

    var products = _productRepository.GetAll();
    var grid = new GridView();
    grid.DataSource = from p in products
    select new
    {
    ProductName = p.ProductName,
    SomeProductId = p.ProductID
    };
    grid.DataBind();
    Response.ClearContent();
    Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
    Response.ContentType = "application/excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    grid.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
    return View("Index");