KoolReport's Forum

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

Laravel 6.0- Excel Export file getting corrupted #1830

Open EmaPromenna opened this topic on on Jan 8, 2021 - 12 comments

EmaPromenna commented on Jan 8, 2021

Using the Larvel 6.0 with the following code for exporting into Excel. the exported file is shown corrupted.

However, when we have checked same code in Laravel 7 and above versions, the file is getting exported and showing correctly.

We are using the following code

public function excel()
        {
            $report = new Report;
            $report->run()->exportToExcel('excel')->toBrowser("myreport.xlsx");
        }

<?php

namespace App\Reports;
use \koolreport\KoolReport;

class Report extends \koolreport\KoolReport
{
    use \koolreport\laravel\Friendship;
    use \koolreport\export\Exportable;
    use \koolreport\excel\ExcelExportable;

    function settings()
    {
        return array(
            "dataSources"=>array(
                "sample_data"=>array(
                    "class"=>'\koolreport\datasources\ArrayDataSource',
                    "dataFormat"=>"table",
                    "data"=>array(
                        array("name","age","income"),
                        array("John",26,50000),
                        array("Marry",29,60000),
                        array("Peter",34,100000),
                        array("Donald",28,80000),
                    )
                )
            )
        );
    }
    function setup()
    {
        $node = $this->src('sample_data')->pipe($this->dataStore('unloadsPivot'));
    }
}

Please Help.

Sebastian Morales commented on Jan 11, 2021

Pls email us the corrupted excel file to support@koolphp.net for us to check it for you.

Sebastian Morales commented on Jan 11, 2021

We have received your attached email. Thanks! Please try the following export command and let us know the result:

    $report->run()->exportToExcel(array(
        'dataStores' => array(
            'unloadsPivot' => array()
        )
    ))
    ->toBrowser("myreport.xlsx");
EmaPromenna commented on Jan 11, 2021

Still Showing the same error we have modified our function as per your suggestion

public function excel()

    {
        $report = new Report;
        $report->run()->exportToExcel(array(
            'dataStores' => array(
                'unloadsPivot' => array()
            )
        ))
        ->toBrowser("myreport.xls");
    }

Please note that we are using Laravel 6 version.

Sebastian Morales commented on Jan 11, 2021

Pls let us know your PHP version and Excel package version. Tks!

EmaPromenna commented on Jan 11, 2021

PHP version 7.3.23, and in the composer.json we have mentioned: "koolreport/export":"*" as per document example

Sebastian Morales commented on Jan 11, 2021

We've just tested exporting to excel using Laravel 6.* and PHP 7.3 and seen no error with the exported file. How did you install Laravel and koolreport/excel, by downloading and extracting or using composer?

EmaPromenna commented on Jan 11, 2021

Yes,We have install using by downloading and extracting the package.

Sebastian Morales commented on Jan 11, 2021

Pls try to install koolreport\excel with composer instead:

https://www.koolreport.com/docs/excel/overview/#installation-by-composer

Then try export to excel again. Tks!

EmaPromenna commented on Jan 11, 2021

Facing the same problem by adding the package through composer.

We are following the this code

Model File: MyReport.php

<?php
namespace App\Reports;
use \koolreport\KoolReport;

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

    function settings()
    {
        return array(
            "dataSources"=>array(
                "sample_data"=>array(
                    "class"=>'\koolreport\datasources\ArrayDataSource',
                    "dataFormat"=>"table",
                    "data"=>array(
                        array("name","age","income"),
                        array("John",26,50000),
                        array("Marry",29,60000),
                        array("Peter",34,100000),
                        array("Donald",28,80000),
                    )
                )
            )
        );
    }


    function setup()
    {
        $node = $this->src('sample_data')->pipe($this->dataStore('unloadsPivot'));
    }
}
``````
Controller File
``````
 public function excel()
        {
            $report = new MyReport;
            $report->run()->exportToExcel(array(
                'dataStores' => array(
                    'unloadsPivot' => array()
                )
            ))
            ->toBrowser("myreport.xls");

        }
````
Excel View  File : MyReportExcel.view.php

``````
<?php
use \koolreport\widgets\google\ColumnChart;

use \koolreport\excel\Table;
use \koolreport\excel\Text;
use \koolreport\excel\PivotTable;
use \koolreport\excel\BarChart;
use \koolreport\excel\LineChart;

$sheetName = 'MyReport';
$reportName = 'MyReport 1';

?>
<meta charset="UTF-8">
<meta name="description" content="test Reports">
<meta name="keywords" content="Excel,HTML,CSS,XML,JavaScript">
<meta name="creator" content="Annet">
<meta name="subject" content="test">
<meta name="title" content="test Kool Report">
<meta name="category" content="Treatment Center">

<div sheet-name="<?php echo $sheetName; ?>">
    <div cell="A1">
        <?php echo $reportName; ?>
    </div>
</div>
Sebastian Morales commented on Jan 11, 2021

Pls try this simple excel view and let us know the result:

<?php

<div sheet-name="sheet1">
    <div cell="A1">
        Hello world
    </div>
</div> 
EmaPromenna commented on Jan 11, 2021

Same problem.

Sebastian Morales commented on Jan 12, 2021

There could have been some pre output that corrupted the excel file. Pls try this:

        error_reporting(0);
        ob_start();       
        $report = new Report;
        $fileHandler = $report->run()->exportToExcel(array(
            'dataStores' => array(
                'unloadsPivot' => array()
            )
        ));
        ob_end_clean();
        $fileHandler->toBrowser("myreport.xls"); 

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
help needed

Excel