KoolReport's Forum

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

Broken file after export to excel #2641

Open 50Grosh opened this topic on on Apr 19, 2022 - 3 comments

50Grosh commented on Apr 19, 2022

Hello, I wonder where I went wrong exporting the data.

I get

// ExportAllData.php
<?php
require_once '../../../../../../wp-load.php';
require_once CLASSES . "reports/config/KoolReportConnectionConfig.php";

use \koolreport\processes\OnlyColumn;
use \koolreport\processes\Join;

class ExportAllData extends KoolReportConnectionConfig
{
    use \koolreport\export\Exportable;
    use \koolreport\excel\ExcelExportable;
    public function settings()
    {
        return $this -> getConfig();
    }

    public function setup()
    {
        $events      = $this->src('...')->query("SELECT *  FROM ...");
        $events_add  = $this->src('...')->query("SELECT *  FROM ...");
        $events_ask  = $this->src('...')->query("SELECT *  FROM ...");
        $users       = $this->src('...')->query("select * from ...");
        $sponsor     = $this->src('...')->query("select * from ...");
        $join        = new Join($events,$users,array("..."=>"..."));
        $join2       = new Join($join,$events_add,array("..."=>"..."));
        $join3       = new Join($join2,$events_ask,array("..."=>"..."));
        $node        = new Join($join3,$sponsor,array("..."=>"..."));
        $node->pipe($this->dataStore('bornDate'));
    }
}

// ExportAllData.view.php
<?php
use \koolreport\excel\Table;
?>
    <div sheet-name="sheet1">
        <div>
            <?php
            Table::create(array(
                "dataSource" => $this->dataStore('bornDate'),
                ));
            ?>
        </div>
    </div>
<?php

// ExportAllDataExcel.view.php
<?php
use \koolreport\excel\Table;
$sheet1 = "Simple report";
?>

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

    <div cell="A1" range="A1:H1" excelstyle='<?php echo json_encode($styleArray); ?>' >
        Enquiry Report
    </div>

    <div>
        <?php
        Table::create(array(
            "dataSource" => $this->dataStore('bornDate'),

        ));
        ?>
    </div>

</div>
//exportAll.php
<?php
include "ExportAllData.php";
$report = new ExportAllData;
$report->run();
$report->exportToExcel('ExportAllDataExcel')->toBrowser("ExportAllData.xls");

I can't understand what's the cause of the error.

Regarts

Sebastian Morales commented on Apr 20, 2022

Pls try to use .xlsx extension. For example:

...->toBrowser("ExportAllData.xlsx");

If there's still an error with the file, email it to support@koolreport.com / support@koolphp.net for us to check it for you. Rgds,

Sebastian Morales commented on Apr 21, 2022

Thank you for your attached excel files. When we open them with text editor we find that there are new line characters at the beginning, before excel content, which might have corrupted the files. To avoid output of unwanted data before excel content pls try the following methods:

1 . Do not use PHP code ending sequence

//exportAll.php
<?php
include "ExportAllData.php";
$report = new ExportAllData;
$report->run();
$report->exportToExcel('ExportAllDataExcel')->toBrowser("ExportAllData.xls"); 
// ?> // It's advisable to not use PHP code ending sequence at the end of pure PHP files because any blank/new lines characters after the end sequence would be output as well.
{blank space output}
{new line output}

2 . Use buffering at the beginning and discard it right before exporting to excel:

<?php
ob_start(); // start buffering instead of direct output
include "ExportAllData.php";
$report = new ExportAllData;
$report->run();
$fileHandler = $report->exportToExcel('ExportAllDataExcel');
ob_end_clean(); // stop buffering and clear buffer
$fileHandler->toBrowser("ExportAllData.xls"); 

Let us know the result. Tks,

50Grosh commented on Apr 25, 2022

ob_start() and ob_end_clean() works. Thank you ;)

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
solved

Excel