使用zend框架将jqgrid数据导出到xsl/excel



im试图将jqgrida数据导出到excel或csv文件中,我遵循了这个示例php+jqgrid+导出到excel,但在我单击按钮导出数据后,什么也没发生。代码来了。。

这是我的list.phtml,我在这里渲染网格,一旦我按下exportdoexcel按钮,它就会影响jqgrid中的数据,并将其发送到action/checkins/export。

<div class="simplebox grid740" style="z-index: 500;">

    <script>
        $(function() {
            $("#toolbar").jqGrid({ 
                caption:"Checkins",
                colNames:['ID','Nome da Tag','Localização da Tag','Cliente','Utilizador','Data da Leitura','Data da Escrita',],
                colModel:[
                    {name:'id_checkin',index:'id_checkin',hidden:true},
                    {name:'tag_nome',index:'tag_nome'},
                      {name:'localizacao',index:'localizacao'},
                        {name:'nome_cli',index:'nome_cli'},
                          {name:'nome_user',index:'nome_user'},
                            {name:'data_leitura',index:'data_leitura'},
                              {name:'data_escrita',index:'data_escrita'},

                ],
                datatype:"json",
                height:421, 
                rownumWidth:40,
                pager:'#ptoolbar',
                rowList:[10,20,30],
                rowNum:10,
                sortname:'id_cliente',
                sortorder:'desc',
                url:'/checkins/list/',
                viewrecords:true,
                width:740,

            });
            $("#toolbar").jqGrid('navGrid','#ptoolbar',{del:false,add:false,edit:false,search:false,excel:true});
            jQuery("#toolbar").jqGrid('navButtonAdd','#ptoolbar',{
            caption:"export", 
            onClickButton : function () { 
            alert('export to excel');
            var mya=new Array();
        mya=$("#toolbar").getDataIDs();  // Get All IDs
        var data=$("#toolbar").getRowData(mya[0]);     // Get First row to get the labels
        var colNames=new Array(); 
        var ii=0;
        for (var i in data){colNames[ii++]=i;}    // capture col names
        var html="";
            for(k=0;k<colNames.length;k++)
            {
            html=html+colNames[k]+"t";     // output each Column as tab delimited
            }
            html=html+"n";                    // Output header with end of line
        for(i=0;i<mya.length;i++)
            {
            data=$("#toolbar").getRowData(mya[i]); // get each row
            for(j=0;j<colNames.length;j++)
                {
             html=html+data[colNames[j]]+"t"; // output each Row as tab delimited
                }
            html=html+"n";  // output each row with end of line
            }
        html=html+"n";  // end of line at the end
         $.ajax({
                            url: '/checkins/export',
                            type: 'POST',
                            data: {param1: html},
                            datatype: "json"

                    });
       } 
});
        });
    </script>
    <table id="toolbar"></table>
    <div id="ptoolbar"></div>
</div>

所以根据firebug的说法,它发送了这样的消息:param1:

id_checkin tag_nome localicao nome_cli nome_user data_leitura data_escrita

1 sasa Maia;波尔图;tesret1212;管理员2013-08-14 15:45:00 2013-08-14 16:00:00

2 sasa Maia;波尔图;tesret1212;tre 2013-08-14 16:06:00 2013-08-14 13:05:00

3 sasa Maia;波尔图;tesret1212;测试2013-08-14 12:15:00 2013-08-14 16:15:00

在我的行动/检查/出口我这样做:

   public function exportAction()
    {
         if($this->_request->isXmlHttpRequest())
        {
            $this->_helper->layout()->disableLayout();
            $this->_helper->viewRenderer->setNoRender(true);
            $param1 = $this->_request->getParam('param1');
             header("Content-Type: application/vnd.ms-excel");
             header("Expires: 0");
             header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
             header("content-disposition: attachment;filename=contacts-download-" . time() . ".xls");
            $buffer = $param1;
            try{
                echo $buffer;
            }catch(Exception $e){
            }
        }
    }

在这里,im接收param1信息并设置标题,然后回显数据。但是要求下载文件的框没有出现,我尝试了其他解决方案,但没有任何效果。。。我错过了什么。通过设置创建excel布局,可以将视图呈现为csv/excel文件?提前感谢Hugo

我实现了与下面相同的东西,它工作得很好下面是我的jqgrid代码

jQuery("#list2").jqGrid('navButtonAdd', '#pager2', {
      caption: "Download", buttonicon:"ui-icon-save", title: "Excel Export",
      onClickButton: function(){ 
          exportExcel();
        }, 
        position:"last"
                      });
      }
       function exportExcel()
       {
  var mya=new Array();
  mya=$("#list2").getDataIDs();  // Get All IDs
  var data=$("#list2").getRowData(mya[0]);     // Get First row to get the labels
  var colNames=new Array(); 
  var ii=0;
  for (var i in data){colNames[ii++]=i;}    // capture col names
  var html="";
      for(k=0;k<colNames.length;k++)
      {
      html=html+colNames[k]+"t";     // output each Column as tab delimited
      }
      html=html+"n";                    // Output header with end of line
  for(i=0;i<mya.length;i++)
      {
      data=$("#list2").getRowData(mya[i]); // get each row
      for(j=0;j<colNames.length;j++)
          {
       html=html+data[colNames[j]]+"t"; // output each Row as tab delimited
          }
      html=html+"n";  // output each row with end of line
      }
  html=html+"n";  // end of line at the end
  console.log(html);
   document.forms[0].csvBuffer.value=html;
   document.forms[0].method='POST';
   document.forms[0].action='analytics.do?method=getDownload';
   document.forms[0].target='_blank';
   document.forms[0].submit();
 } 

在html代码中添加以下三行

<form id='_export' method="post" action="">
<input type="hidden" name="csvBuffer" id="csvBuffer" value="" />
</form>

以及将csv文件下载为的服务器代码

 public void getDownload(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) throws Exception{
    PrintWriter out = response.getWriter();
         String param1 = request.getParameter("csvBuffer");
         response.setContentType("application/octet-stream");
         response.setHeader("Content-Disposition",
         "attachment;filename=downloadfilename.csv");
        try{
            System.out.println(param1);
            out.print(param1);
        }catch(Exception $e){
        }
} //end of getDownload method

所以它以csv格式下载文件。

最新更新