Excel Exporting Charts in Table

Exporting table with chart column

productNameQ1Q2Q3Q4Chart
1937 Lincoln Berline 21151.11 21166.55 19598.63 40647.19
1936 Mercedes-Benz 500K Special Roadster 12146.66 8092.53 7590.46 18248.63
1952 Alpine Renault 1300 43938.09 34943.71 33460.91 77675.24
1962 LanciaA Delta 16V 32472.34 22874.64 19918.52 47857.46
1958 Setra Bus 27629.36 23291.46 20586.1 47578.3

This example demonstrates how to export table to an excel file.

<?php
    \koolreport\excel\Table::create(array(
        ...
        'columns' => [
            ...
            'Chart' => [
                'formatValue' => function($value, $row, $ckey, $meta) {
                    return \koolreport\excel\PieChart::create([
                        "dataSource" => [
                            ["Quarter", "Sales"],
                            ["Q1", $row['Q1']],
                            ["Q2", $row['Q2']],
                            ["Q3", $row['Q3']],
                            ["Q4", $row['Q4']]
                        ],
                        "columns" =>[
                            'Quarter', "Sales"
                        ],
                    ], true);
                },
            ],
        ]
    ));
?>
<?php
require_once "MyReport.php";

$report = new MyReport;
$report->run()->render();
<?php
require_once "../../../load.koolreport.php";

use \koolreport\processes\Map;
use \koolreport\processes\Limit;
use \koolreport\processes\Shuffe;
use \koolreport\processes\Filter;
use \koolreport\cube\processes\Cube;
use \koolreport\pivot\processes\Pivot;

class MyReport extends koolreport\KoolReport
{
    use \koolreport\export\Exportable;
    use \koolreport\excel\ExcelExportable;
    use \koolreport\excel\BigSpreadsheetExportable;

    function settings()
    {
        return array(
            "dataSources" => array(
                "dollarsales"=>array(
                    'filePath' => '../../../databases/customer_product_dollarsales2.csv',
                    'fieldSeparator' => ';',
                    'class' => "\koolreport\datasources\CSVDataSource"      
                ), 
            )
        );
    }    function setup()
    {
        $node = $this->src('dollarsales')
        //->query('select *, dollar_sales as dollar_sales2 from customer_product_dollarsales2')
        ->pipe(new Map([
            '{value}' => function($row, $meta) {
                $row['orderQuarter'] = 'Q' . $row['orderQuarter'];
                return $row;
            },
            '{meta}' => function($meta) {
                $meta['columns']['orderDate']['type'] = 'datetime';
                $meta['columns']['orderQuarter']['type'] = 'string';
                return $meta;
            }
        ]))
        ;

        $node
        ->pipe(new Limit(array(
            5, 0
        )))
        ->pipe(new Map([
            "{meta}" => function($meta) {
                $cMeta = & $meta["columns"]["dollar_sales"];
                $cMeta["footer"] = "sum";
                $cMeta["type"] = "number";
                // print_r($meta); exit;
                return $meta;
            }
        ]))
        ->pipe($this->dataStore('orders'));

        $node->pipe(new Cube(array(
            "rows" => "customerName",
            "column" => "orderQuarter",
            "sum" => "dollar_sales",
        )))
        ->pipe(new Limit(array(
            5, 0
        )))
        ->pipe($this->dataStore('salesQuarterCustomer'));

        $node->pipe(new Cube(array(
            "rows" => "productName",
            "column" => "orderQuarter",
            "sum" => "dollar_sales",
        )))
        ->pipe(new Limit(array(
            5, 0
        )))
        ->pipe($this->dataStore('salesQuarterProduct'));

        $node
        ->pipe(new Filter(array(
            array('customerName', '<', 'Au'),
            array('orderYear', '>', 2003),
        )))
        ->pipe(new Pivot(array(
            "dimensions" => array(
                "column" => "orderYear, orderQuarter",
                "row" => "customerName, productLine",
            ),
            "aggregates" => array(
                "sum" => "dollar_sales",
            ),
        )))
        ->pipe($this->dataStore('salesPivot'));
    }
}
<?php

use \koolreport\pivot\widgets\PivotTable;
use \koolreport\widgets\koolphp\Table;
use \koolreport\sparklines\Bar;
use \koolreport\sparklines\Box;
use \koolreport\sparklines\Bullet;
use \koolreport\sparklines\Line;
use \koolreport\sparklines\Pie;
use \koolreport\sparklines\Tristate;
?>
<div class="report-content">
	<div style='text-align: center;margin-bottom:30px;'>
		<h1>Excel Exporting Charts in Table</h1>
		<p class="lead">Exporting table with chart column</p>
		<form method="post">
			<button type="submit" class="btn btn-primary" formaction="export.php?type=excel">Download Excel</button>
		</form>
	</div>
	<div class='box-container'>
		<div>
			<?php
			Table::create(array(
				"dataSource" => $this->dataStore('salesQuarterProduct'),
				"columns" => array(
					"productName",
					"Q1",
					"Q2",
					"Q3",
					"Q4",
					"Chart" => [
						"formatValue" => function ($value, $row, $cKey) {
							return Pie::create(array(
								"data" => [
									$row['Q1'],
									$row['Q2'],
									$row['Q3'],
									$row['Q4']
								],
								"width" => "60px",
								"height" => "60px",
							));
						},
					],
				),
			));
			?>
		</div>
	</div>
</div>
<?php
    use \koolreport\excel\Table;
    use \koolreport\excel\PivotTable;
    use \koolreport\excel\BarChart;
    use \koolreport\excel\LineChart;

    $sheet1 = "Sales by Customer";
?>
<meta charset="UTF-8">
<meta name="description" content="Free Web tutorials">
<meta name="keywords" content="Excel,HTML,CSS,XML,JavaScript">
<meta name="creator" content="John Doe">
<meta name="subject" content="subject1">
<meta name="title" content="title1">
<meta name="category" content="category1">

<div sheet-name="<?php echo $sheet1; ?>">
    <div>Orders Table</div>

    <div>
        <?php
        Table::create(array(
            "dataSource" => $this->dataStore('salesQuarterProduct'),
            // "dataSource" => 'orders',
            'columns' => [
                'productName' => [
                    'label' => 'Info-Product-Name',
                    'width' => 60, // overrides outside "columnWidth" property
                ],
                'Q1', 'Q2', 'Q3', 'Q4',
                'Chart' => [
                    'formatValue' => function($value, $row, $ckey, $meta) {
                        return \koolreport\excel\PieChart::create([
                            "dataSource" => [
                                ["Quarter", "Sales"],
                                ["Q1", $row['Q1']],
                                ["Q2", $row['Q2']],
                                ["Q3", $row['Q3']],
                                ["Q4", $row['Q4']]
                            ],
                            "columns" =>[
                                'Quarter', "Sales"
                            ],
                        ], true);
                    },
                ],
            ],
            'rowHeight' => '50',
            // 'rowHeight' => function($row, $rowIndex) {
            //     // return 'auto';
            //     return 10 * ($rowIndex + 1);
            // },
            // 'columnWidth' => '50',
            'columnWidth' => function($columnName, $columnIndex) {
                if ($columnName === 'Chart') return '30';
                else return 'auto';
            },
            // 'columnAutoSize' => false,
        ));
        ?>
    </div>
    
</div>
<?php
include "MyReport.php";
// var_dump($_GET); exit;
$report = new MyReport;
$report->run();
$type = $_GET['type'];
if ($type === 'excel') {
    $report->exportToExcel('MyReportExcel')->toBrowser("MyReport.xlsx");
} else if ($type === 'bigspreadsheet') {
    $report->exportToXLSX('MyReportExcel')->toBrowser("MyReport.xlsx");
}
// $report->exportToExcel([
//     "dataStores" => [
//         "orders" => [
//             "filtering" => function($row, $index) { 
//                 if (stripos($row['customerName'], "Baane Mini Imports") !== false)
//                     return false;
//                 return true;
//             },
//             "sorting" => ['dollar_sales' => 'desc'],
//             "paging" => [5, 2],
//             "showHeader" => false, //default: true
//             "showBottomHeader" => true, //default: false
//             "showFooter" => true, //default: false
//             "map" => [
//                 "header" => function($colName) { return $colName; },
//                 "bottomHeader" => function($colName) { return $colName; },
//                 "cell" => function($colName, $value, $row) { return $value; },
//                 "footer" => function($colName, $footerValue) { return $footerValue; },
//             ],
//             "excelStyle" => [
//                 "header" => function($colName) { 
//                     return [
//                         'font' => [
//                             'italic' => true,
//                             'bold' => false,
//                             'color' => [
//                                 'rgb' => '808080',
//                             ]
//                         ],
//                     ]; 
//                 },
//                 "bottomHeader" => function($colName) { return []; },
//                 "cell" => function($colName, $value, $row) { 
//                     return [
//                         'font' => [
//                             'italic' => true,
//                             'color' => [
//                                 'rgb' => '808080',
//                             ]
//                         ],
//                     ]; 
//                  },
//                 "footer" => function($colName, $footerValue) { return []; },
//             ]
//         ]
//     ]
// ])
// ->toBrowser("MyReport.xlsx");
customerNameproductNameproductLineorderDateorderDayorderMonthorderYearorderQuarterdollar_sales
Vitachrome Inc. 1937 Lincoln Berline Vintage Cars 2003-01-10 00:00:00 10 1 2003 1 3726.45
Vitachrome Inc. 1936 Mercedes-Benz 500K Special Roadster Vintage Cars 2003-01-10 00:00:00 10 1 2003 1 1768.33
Baane Mini Imports 1952 Alpine Renault 1300 Classic Cars 2003-01-29 00:00:00 29 1 2003 1 5571.8
Baane Mini Imports 1962 LanciaA Delta 16V Classic Cars 2003-01-29 00:00:00 29 1 2003 1 5026.14
Baane Mini Imports 1958 Setra Bus Trucks and Buses 2003-01-29 00:00:00 29 1 2003 1 3284.28
Baane Mini Imports 1940 Ford Pickup Truck Trucks and Buses 2003-01-29 00:00:00 29 1 2003 1 3307.5
Baane Mini Imports 1926 Ford Fire Engine Trucks and Buses 2003-01-29 00:00:00 29 1 2003 1 1283.48
Baane Mini Imports 1913 Ford Model T Speedster Vintage Cars 2003-01-29 00:00:00 29 1 2003 1 2489.13
Baane Mini Imports 1934 Ford V8 Coupe Vintage Cars 2003-01-29 00:00:00 29 1 2003 1 2164.4
Baane Mini Imports 18th Century Vintage Horse Carriage Vintage Cars 2003-01-29 00:00:00 29 1 2003 1 2173

What People Are Saying

"KoolReport helps me very much in creating data report for my corporate! Keep up your good work!"
-- Alain Melsens

"The first use of your product. I was impressed by its easiness and powerfulness. This product is a great and amazing."
-- Dr. Lew Choy Onn

"Fantastic framework for reporting!"
-- Greg Schneider

Download KoolReport Get KoolReport Pro