views:

2977

answers:

5

Somebody knows a way to export the data from a jqgrid to excel?

I want to do a report using this jqgrid that i think is awsome. But i need to save or print this report somehow, because is information to be keeped. Somebody knows any way??

+2  A: 

Here is a demo page that may be helpful: http://www.trirand.com/blog/phpjqgrid/examples/functionality/excel/default.php

Justin Ethier
A: 

Here i’ve made a self-written PHP / Ajax Datagrid component using JQGrid (based on JQuery). This library can generate fully featured CRUD application in record time.

include("jqgrid.php"); $g = new jqgrid(); $grid["caption"] = "My Sample Grid"; // set grid customizable params $g->set_options($grid); $g->table = "tags"; // db table for CRUD operations.

// You can also specify SQL query for displaying data (download code with examples) // $g->select_command = "select f1,f2,f3 from tags";

$out = $g->render("my_grid_1"); // render grid

... and in HTML after including JS files ...

This code will result in fully functional Jquery Grid (JqGrid) with …

* Add
* Edit
* Delete
* Search
* Auto-filter
* Sort
* Pagination
* Export
* Multiple Themes (ThemeRoller)
* and almost all features available for FREE

http://azgtech.wordpress.com/2010/08/01/jqgrid-php-datagrid-component-free/

Azghanvi
A: 

This is my approach, just add this code to your js/html file

$("#list").jqGrid('navGrid', '#pager',{view:true, del:false, add:false, edit:false, excel:true})
                .navButtonAdd('#pager',{
                                caption:"Export to Excel", 
                                buttonicon:"ui-icon-save", 
                                onClickButton: function(){ 
                                  exportExcel();
                                }, 
                                position:"last"
                            });

        function exportExcel()
        {
            var mya=new Array();
            mya=$("#list").getDataIDs();  // Get All IDs
            var data=$("#list").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(i=0;i<mya.length;i++)
                {
                data=$("#list").getRowData(mya[i]); // get each row
                for(j=0;j<colNames.length;j++)
                    {
                    html=html+data[colNames[j]]+"\t"; // output each column as tab delimited
                    }
                html=html+"\n";  // output each row with end of line

                }
            html=html+"\n";  // end of line at the end
            document.forms[0].csvBuffer.value=html;
            document.forms[0].method='POST';
            document.forms[0].action='csvExport.php';  // send it to server which will open this contents in excel file
            document.forms[0].target='_blank';
            document.forms[0].submit();
        }

PHP script

header('Content-type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=file.xls");
header("Pragma: no-cache");

$buffer = $_POST['csvBuffer'];

try{
    echo $buffer;
}catch(Exception $e){

}
Felix Guerrero
A: 

Thanks for share your code Felix Guerrero. I have a problem: when I run your code I get this error: document.forms[0].csvBuffer is undefined, can you help me to fix this?

Ignacio Guillen