KoolReport's Forum

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

Unreadable content on csv/xlsx export. #1049

Open Jeremy Wheat opened this topic on on Aug 19, 2019 - 8 comments

Jeremy Wheat commented on Aug 19, 2019

On export excel, I got html content on downloaded file. Here are some sample

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>


<meta http-equiv="Content-Type" content="text/html
<meta name="google" content="notranslate">
<link rel="icon" href="/images/favicon2.png" type="image/png">
<title>hospital 1462: Export?</title>

And Also at the bottom of file I got some unreadable content. Sample :

�QĿi!��K�y3�J<���Z1�0?Y�L%zV
c��Ib7����a/l٥P1:�q�r��j��j0A����u�""���(�
�{���1F�^ʠ�2��l�$���-}�y����Î��O��v�y�
�h����[��4�ი��]��yN�
-'
V�)�#��EF^6��n��8q"K��H��>_ׄ���eƏ�<⇄�Ud�v��
T�PK��O�$�V��xl/_rels/workbook.xml.rels���j�0D��
��ZvZJ)�s)�\[����LlIh7m��UH��Ĭؙ�H���8�OL���*J�M���|4��g��[=�
&$��w�74�r}$�M<)p��EJ2GME���MҨ9��ɨ�^w(We�$���O��
��V �)��C��_�9���J�$���/�:dG]d���WK�s��s�<�[�V�tB��)?�����0_!��!��4�E�ǩy���PK��O���x�9docProps/app.xml�S�n�0��+�9�0���H7�b��gV�c��$�����'

Do you know why this is happening?

I hope you can help me.

Thanks

David Winterburn commented on Aug 20, 2019

Hi Jeremy,

Would you please post your php code for us to check it for you? Thanks!

Jeremy Wheat commented on Aug 20, 2019

Hi David,

I'm taking reference from https://www.koolreport.com/examples/reports/excel/excel_template/

Following is php code :

// MyReport.excel.php In this file above code of style is same. After that I made some changes :

<div>Orders</div>

    <div>
        <?php
        Table::create(array(
            "dataSource" => $this->dataStore('patient'),
            // "dataSource" => 'orders',
            "headersExcelStyle" => [
                'customerName' => [
                    'font' => [
                        'italic' => true,
                        'color' => [
                            'rgb' => '808080',
                        ]
                    ],
                ]
            ],
            "columnsExcelStyle" => [
                'customerName' => [
                    'font' => [
                        'italic' => true,
                        'color' => [
                            'rgb' => '808080',
                        ]
                    ],
                ]
            ],

        ));
        ?>
    </div>

    <?php
        //$this->dataStore('patient')
        $datastore = 'patient';
            ?>
            <div>
                <?php //echo $title; ?>
            </div>
            <div>
                <?php
                Table::create(array(
                    "name" => $name,
                    "dataSource" => $this->dataStore($datastore),
                    // "dataSource" => $datastore,
                    "columns" =>[
                        $column, "Q1", "Q2", "Q3"
                    ],
                ));
                ?>
            </div>
        <?php 
    ?>

    
</div>

And on export.php

<?php
include "MyReport.excel.php";
$report = new MyReport;
$report->run()->exportToExcel()->toBrowser("myreport.csv");
exit;

It gives error either there is already imported in this at the top of php file:

use \koolreport\excel\Table;
( ! ) Fatal error: Uncaught Error: Class 'koolreport\excel\Table' not found in /var/www/localStaging/local_staging/kreport/MyReport.excel.php on line 85
( ! ) Error: Class 'koolreport\excel\Table' not found in /var/www/localStaging/local_staging/kreport/MyReport.excel.php on line 85

Hope you can understand.

Thanks

David Winterburn commented on Aug 20, 2019

Hi Jeremy,

Please try to rename the excel view file to MyReportExcel.view.php and use the following code in export.php:

include "MyReport.php"; //not MyReportExcel
$report = new MyReport();
$report->run();
$report->exportToExcel('MyReportExcel')->toBrowser("MyReport.xls");
exit;

Hope this solves you problem. Thanks!

Jeremy Wheat commented on Aug 20, 2019

Hi David, Thank you very much. It works for me.

But Now, on exporting xlsx file I got only header content (header include for by website). it should be my table content that shown on page on exported file. Here you can see the sample of xlsx image :

Can you help me out in resolving it.

Thanks

David Winterburn commented on Aug 21, 2019

Hi Jeremy,

Would you please post the entire content of the file MyReportExce.view.php? Thanks!

Jeremy Wheat commented on Aug 21, 2019

Hi David,

Here is the code :

<?php // MyReportExcel.view.php
use \koolreport\pivot\widgets\PivotTable;
use \koolreport\widgets\koolphp\Table;



// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);

?>
    <?php
    $styleArray = [
        'font' => [
            'name' => 'Calibri', //'Verdana', 'Arial'
            'size' => 30,
            'bold' => false,
            'italic' => FALSE,
            'underline' => 'none', //'double', 'doubleAccounting', 'single', 'singleAccounting'
            'strikethrough' => FALSE,
            'superscript' => false,
            'subscript' => false,
            'color' => [
                'rgb' => '000000',
                'argb' => 'FF000000',
            ]
        ],
        'alignment' => [
            'horizontal' => 'general',//left, right, center, centerContinuous, justify, fill, distributed
            'vertical' => 'bottom',//top, center, justify, distributed
            'textRotation' => 0,
            'wrapText' => false,
            'shrinkToFit' => false,
            'indent' => 0,
            'readOrder' => 0,
        ],
        'borders' => [
            'top' => [
                'borderStyle' => 'none', //dashDot, dashDotDot, dashed, dotted, double, hair, medium, mediumDashDot, mediumDashDotDot, mediumDashed, slantDashDot, thick, thin
                'color' => [
                    'rgb' => '808080',
                    'argb' => 'FF808080',
                ]
            ],
            //left, right, bottom, diagonal, allBorders, outline, inside, vertical, horizontal
        ],
        'fill' => [
            'fillType' => 'none', //'solid', 'linear', 'path', 'darkDown', 'darkGray', 'darkGrid', 'darkHorizontal', 'darkTrellis', 'darkUp', 'darkVertical', 'gray0625', 'gray125', 'lightDown', 'lightGray', 'lightGrid', 'lightHorizontal', 'lightTrellis', 'lightUp', 'lightVertical', 'mediumGray'
            'rotation' => 90,
            'color' => [
                'rgb' => 'A0A0A0',
                'argb' => 'FFA0A0A0',
            ],
            'startColor' => [
                'rgb' => 'A0A0A0',
                'argb' => 'FFA0A0A0',
            ],
            'endColor' => [
                'argb' => 'FFFFFF',
                'argb' => 'FFFFFFFF',
            ],
        ],
    ];
    ?>
    
    <div cell="A1" range="A1:H1" excelstyle='<?php echo json_encode($styleArray); ?>' >
        Custom Report
    </div>

    <div>Orders</div>

    <div>
        <?php

        Table::create(array(
            "dataSource" => $this->dataStore('table'),
            // "dataSource" => 'orders',
            "headersExcelStyle" => [
                'customerName' => [
                    'font' => [
                        'italic' => true,
                        'color' => [
                            'rgb' => '808080',
                        ]
                    ],
                ]
            ],
            "columnsExcelStyle" => [
                'customerName' => [
                    'font' => [
                        'italic' => true,
                        'color' => [
                            'rgb' => '808080',
                        ]
                    ],
                ]
            ],

        ));
        ?>
    </div>

    <?php
        $datastore = 'table';
        $title = 'Test Records';
        $name = 'Test Records';
        $column = 'test';
            ?>
            <div>
                <?php echo $title; ?>
            </div>
            <div>
                <?php
                Table::create(array(
                    "name" => $name,
                    "dataSource" => $this->dataStore($datastore),
                    // "dataSource" => $datastore,
                    "columns" =>[
                        $column, "Q1", "Q2", "Q3"
                    ],
                ));
                ?>
            </div>
        <?php 
    ?>

    
</div>

I think it will also help. Here is code of Myreport.php from where I get the view of first page.

<?php

require_once "../vendor/koolreport/core/autoload.php";

include realpath(dirname(__FILE__)."/../").'/includes/master_header.php'; // Website header

include realpath(dirname(__FILE__)."/").'/common_connection.php'; //database connection file


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



    public function settings()
    {   
        $common_connection = new common_connection(); // Separated out connection for common use of multiple view
        return $common_connection->connection();
    }


    function setup()
    {
        $qry = "SELECT col1,col2,col3 FROM table ORDER BY `col1` ASC limit 0,11"; //This is not original query
        $report_qry = $this->src('database');
        $report_qry->query($qry);
        $report_qry->pipe($this->dataStore('table'));
    }
}

And MyReport.view.php

<?php
use \koolreport\pivot\widgets\PivotTable;
use \koolreport\widgets\koolphp\Table;

	$get_heading = ["col1","col2","col3"];
?>
<div sheet-name="Sheet">
<style>
.page-header {
     margin: 0; 
}
</style>
<div class="report-content">
	<div style='text-align: center;margin-bottom:30px;'>
        <h1>Excel Exporting Template</h1>
        <p class="lead">Using template to create excel report</p>
		<form>
			<button type="submit" class="btn btn-primary" formaction="export.php">Download Excel</button>
		</form>
	</div>
	<div class='box-container'>
		<div>
        <?php

	$columns = array();
        $data_field = $this->dataStore('table')->data();        
        $i=0;
        foreach($data_field[0] as $key=>$value)
        {
            $columns[$key] = array(
                "label" => $get_heading[$i],

            );
            $i++;
        }
        Table::create(array(
	    	"dataStore"=>$this->dataStore('table'),
	         "columns" => $columns,
	        "paging"=>array(
	            "pageSize"=>10,
	        ),
	         // "showHeader"=>false,
	    "cssClass"=>array(
	        "table"=>"table table table-bordered table-striped",
	    )
	));
	?>
		</div>
	</div>
</div>

Can you help me out to know what I'm doing wrong?

Thanks

David Winterburn commented on Aug 21, 2019

Hi Jeremy,

I think there're 2 problems with your files.

  1. The excel view file lacks the most outside divs which represent sheets. The structure of the excel template should look like this:
<div sheet-name="sheet1">
    <div>{text, table or pivot content}</div>
    <div>{text, table or pivot content}</div>
</div>

<div sheet-name="sheet2">
    <div>{text, table or pivot content}</div>
    <div>{text, table or pivot content}</div>
</div>
  1. The MyReport.php should not include master_header.php. If you need to include header, menu, etc they should be in your index.php or view file.

Please fix these 2 issues and let us know the result. Thanks!

Jeremy Wheat commented on Aug 21, 2019

That works!! Thanks David.

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
solved

Export