KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Koolreport datagrid export to excel #2414

Open Ron opened this topic on on Nov 3, 2021 - 9 comments

Ron commented on Nov 3, 2021

I have a Datagrid widget that I want to export to excel. When I use the ExportToExcel koolreport library it is not displayed correctly. Is there an option to use the datatables export library locally. I am using rowspan, colspan, total rows and when I export koolreports library I don't see any of it in the excel file.

Sebastian Morales commented on Nov 3, 2021

Excel's Table widget does have a "rowGroup" property which might suit your case:

https://www.koolreport.com/examples/reports/excel/table_rowgroup/

Ron commented on Nov 5, 2021

I am using a datatable and not a table. the export should be done after the datatable was initialized in the client side.

DataTables::create(array(
                "name" => "myTable",
                "language" => "he", //use 'DataTables.de.json'
                "emptyValue" => "", // use an empty string as filler for empty values
                "dataSource"=>$this->dataStore("st"),
                "options"=>array(
                    'ordering' => false,
                    "searching"=>true,
                    "paging"=>true,
                    'columnDefs' => array(
            			array(
            				//'visible' => false,
            				// 'targets' => [$this->monthDays+6,2,3], //hide the first column
            			)
            		),
            		'rowGroup' => [
                              'endRender' => "function ( rows, group ) {
                                    var totalA = rows
                                        .data()
                                        .pluck($this->monthDays+4)
                                        .reduce( function (a, b) {
                                            return a + b.replace(/[^\d]/g,'')*1;
                                    }, 0);

                                    var totalB = rows
                                        .data()
                                        .pluck($this->monthDays+5)
                                        .reduce( function (a, b) {
                                            return a + b.replace(/[^\d]/g,'')*1;
                                    }, 0);

                                    var totalC = rows
                                        .data()
                                        .pluck($this->monthDays+6)
                                        .reduce( function (a, b) {
                                            return a + b.replace(/[^\d]/g,'')*1;
                                        }, 0);

                                    var totalD = rows
                                        .data()
                                        .pluck($this->monthDays+7)
                                        .reduce( function (a, b) {
                                            return a + b.replace(/[^\d]/g,'')*1;
                                        }, 0);

                                    return $('<tr/>')
                                        .append( '<td colspan='+($this->monthDays+4)+'><b>$group_label '+group+'</b></td>' )
                                        .append( '<td class=text-center><b>'+totalA+'</b></td>' )
                                        .append( '<td class=text-center><b>'+totalB+'</b></td>' )
                                        .append( '<td class=text-center><b>'+totalC+'</b></td>' )
                                        .append( '<td class=text-center><b>'+totalD+'</b></td>' )
                                        .append( '<td></td>' )
                                }",

                    ],
                ),
                "groupCellsInColumns" => [0, 1, 2], // group similar cells in 1st and 3rd columns
                'complexHeaders' => true,
                'headerSeparator' => ' - ',
                'cssClass'=>array(
                    'table'=>'table table-bordered',
                    'tr'=>'cssItem',
                    'td'=>function($row,$colName)
                    {
                        return in_array($colName, array('substitute_teacher_id','id_number','substituted_teacher','group_name')) ? 'text-right' : 'text-center';
                        // if ($colName == 12) {
                        //     return 'table-dark';
                        // }
                    },
                    'th'=>function($colName)
                    {
                        return in_array($colName, array('substitute_teacher_id','id_number','substituted_teacher','group_name')) ? 'table-dark text-right' : 'table-dark text-center';
                    },
                ),
            ));
Sebastian Morales commented on Nov 5, 2021

Row group in DataTables, which renders in html, has nothing to do with row group in excel. If you want to have row group in excel you must use the excel's Table widget with its rowGroup property.

Ron commented on Nov 5, 2021

I am using the datatable widget beta and it has the groupCellsInColumns property. Table widget in excel does not include this option. that is why the excel's table widget with rowgroup property will not help.

Sebastian Morales commented on Nov 5, 2021

I see. There is, however, a similar feature for excel Table (and core Table) called removeDuplicate:

https://www.koolreport.com/docs/excel/excel_widgets/#table-widget-(version-%3E=-6.0.0)-removeduplicate-(version-%3E=-8.5.0)

With this one you specify the column names instead of column orders:

    //MyReportExcel.view.php
    \koolreport\excel\Table::create(array(
        ...
        "removeDuplicate" => [ "columnName1", "columnName2" ],
    ));

We will make it works with column orders as well in the future releases. Rgds,

Ron commented on Nov 5, 2021

removeDuplicates does not fit my needs. that is why I waited for the best version to come out with the groupCellsInColumns. I need to know how do I export the generated datatable widget to excel in the client browser.

Sebastian Morales commented on Nov 5, 2021

Pls describe exactly why removeDuplicate doesn't fit your need. Is there any difference in visual or is it because it uses column names instead of column orders, etc.

Ron commented on Nov 6, 2021

As you can see in the report view code I have subtotal calculation. don't know if it can be done in table widget. if you have a way to show me I can I shift from the Datatable to table cnad solve all the issues and I will be more the happy

Sebastian Morales commented on Nov 8, 2021

I still don't get the exact visual of the kind of row grouping you want for excel. Remember that DataTables' features for html web does not magically map exactly to excel Table. If you can provide some screenshots with description of what you want we will see if we could implement it. Rgds,

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
None yet

None