0

我试图将 jqgrida 数据导出到 excel 或 csv 文件,我正在按照这个示例 php + jqgrid + 导出到 excel,但是在我单击按钮导出数据后没有任何反应。这是代码..

这是我的list.phtml,我在其中渲染网格,一旦我按下按钮导出做excel,它就会从jqgrid中获取数据并将其发送到操作/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>

所以根据萤火虫它发送这个:param1:

id_checkin tag_nome localizacao nome_cli nome_user data_leitura data_escrita

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

2 莎莎玛雅;波尔图; 特雷特1212;树 2013-08-14 16:06:00 2013-08-14 16:05:00

3 莎莎玛雅;波尔图; 特雷特1212;测试 2013-08-14 12:15:00 2013-08-14 16:15:00

在我的操作 /checkins/export 中我这样做:

   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){

            }
        }
    }

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

4

1 回答 1

0

我正在实现与下面相同的东西,它工作正常下面是我的 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 格式。

于 2013-10-08T07:01:48.390 回答