Excel Exporting Image and Hyperlink Columns

Exporting excel table with image and hyperlink columns

productNamedollar_salesimageurl
1937 Lincoln Berline 3,726 Example site
1936 Mercedes-Benz 500K Special Roadster 1,768 Example site
1952 Alpine Renault 1300 5,572 Example site
1962 LanciaA Delta 16V 5,026 Example site
1958 Setra Bus 3,284 Example site

This example demonstrates how to export table with image and hyperlink columns to an excel file.

<div>
    <?php
    \koolreport\excel\Table::create(array(
        'columns' => [
            'image' => [
                'formatValue' => function($value, $row, $ckey, $meta) {
                    return '/Users/dongnl/Downloads/report.jpg';
                },
                'cellType' => 'image',
            ],
            'url' => [
                'formatValue' => function ($value, $row, $ckey, $meta) {
                    return 'https://www.example.com';
                },
                'cellType' => 'hyperlink',
            ]
        ],
        ...
    ));
    ?>
</div>
<?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\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'));

    }
}
<?php
use \koolreport\pivot\widgets\PivotTable;
use \koolreport\widgets\koolphp\Table;
?>
<div class="report-content">
	<div style='text-align: center;margin-bottom:30px;'>
        <h1>Excel Exporting Image and Hyperlink Columns</h1>
        <p class="lead">Exporting excel table with image and hyperlink columns</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('orders'),
				"columns"=>array(
					"productName",
					"dollar_sales"=>array(
						"type"=>"number",
					),
					'image' => [
						'formatValue' => function($value, $row, $ckey) {
							return '<img src="../../../assets/images/bar.png" height="40px" />';
						},
					],
					'url' => [
						'formatValue' => function ($value, $row, $ckey) {
							return '<a href="https://www.example.com">Example site</a>';
						},
					]
				),
				"paging"=>array(
					"pageSize"=>5
				)
			));
			?>
		</div>
	</div>
</div>
<?php
    $sheet1 = "Sales by Customer";
?>
<div sheet-name="<?php echo $sheet1; ?>">
    <div>Orders Table</div>

    <div>
        <?php
        \koolreport\excel\Table::create(array(
            "dataSource" => $this->dataStore('orders'),
            'columns' => [
                'productName' => [
                    'label' => 'Info-Product-Name',
                    'width' => 60, // overrides outside "columnWidth" property
                ],
                'dollar_sales' => [
                    'label' => "Sales",
                ],
                'image' => [
                    'formatValue' => function($value, $row, $ckey, $meta) {
                        return \koolreport\excel\Image::create([
                            'path' => '../../../assets/images/bar.png',

                            // use offsetX, offsetY as margin
                            'offsetX' => 5, // default: 0
                            'offsetX2' => -5, // default: 0
                            'offsetY' => 5, // default: 0
                            'offsetY2' => -5, // default: 0
                            
                            // 'name' => 'name', // default: ''
                            // 'description' => 'description', // default: ''
                            // 'roration' => 0, // default: 0
                            // 'hyperlink' => null, //Hyperlink object, default: null
                            // 'shadow' => null, //Shadown object, default: null
                            'shadowVisible' => true, // default: false
                            'shadowDirection' => 45, // default: 0
                        ], true);
                    }
                ],
                'url' => [
                    'formatValue' => function ($value, $row, $ckey, $meta) {
                        return \koolreport\excel\Hyperlink::create([
                            'url' => 'https://www.example.com',
                            'text' => 'Example site'
                        ], true);
                    },
                ]
            ],
            // 'rowHeight' => '50',
            'rowHeight' => function($row, $rowIndex) {
                // return 'auto';
                return 10 * ($rowIndex + 1);
            },
            // 'columnWidth' => '30',
            'columnWidth' => function($columnName, $columnIndex) {
                if ($columnName === 'image') return '10';
                else return 'auto';
            },
            // 'columnAutoSize' => false,
        ));
        ?>
    </div>
    
</div>
<?php
include "MyReport.php";
$report = new MyReport;
$report->run();
$report->exportToExcel('MyReportExcel')->toBrowser("MyReportExcel.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