KoolReport's Forum

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

Cannot modify header information #1211

Open Sowmya opened this topic on on Dec 11, 2019 - 13 comments

Sowmya commented on Dec 11, 2019

Dear KoolReport team,

I am facing this issue from last 2 days. Already I posted yesterday in Forum No#1210. But I didn't get reply. I want to Download Excel file. It genrates the Table structure, But below of table structure it shows the following error

Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\CI3\application\libraries\koolreport\core\src\KoolReport.php:455)

and I want to make download the report in Excel,CSV or ODS.

How can I overcome this error. Please help me.

class Excel extends CI_Controller
{
	
   	public function index()
   	{
   		$this->load->helper('url');
   		$this->load->database('MainiDM');
   		
   		include APPPATH."reports\ExcelReport_Ex.php";
                $report = new ExcelReport_Ex;
                $report->run()->render();
         

                $type = isset($_GET['type'])? $_GET['type'] : 'XLSX';

                 if($type === 'XLSX')
                 {
                    $report->exportToXLSX('ExcelReportSpreadsheet')-    >toBrowser("ExcelReport.xlsx");
         }

         elseif($type === 'ODS')
         {
            $report->exportToODS('ExcelReportSpreadsheet')->toBrowser('ExcelReport.ods');
         }

         elseif($type === 'CSV')
         {
            $report->exportToCSV('ExcelReportSpreadsheet')->toBrowser('ExcelReport.csv');
           
         }

   	}
}
class ExcelReport_Ex extends \koolreport\KoolReport
{
	use \koolreport\codeigniter\Friendship;
    use \koolreport\excel\BigSpreadsheetExportable;

	public function settings()
	{
		return array(
			
            "dataSources"=>array(
                "excel_export"=>array(
                    "connectionString"=>"sqlsrv:Server=SQLPC;Database=MainiDM",
                    "username"=>"sa",
                    "password"=>"sa1234",
                    "charset"=>"utf8"
                )
            )
        );
	}

    public function setup()
    {
        $this->src('excel_export')
        ->query("SELECT * from EnquiryDataView")
        ->pipe($this->dataStore('excel_data'));
    }
}
<form method="post">
          <button type="submit" class="btn btn-primary" formaction="<?php echo site_url();?>\Excel?type=XLSX">Download XLSX</button>
          <button type="submit" class="btn btn-primary" formaction="export.php?type=ODS">Download ODS</button>
          <button type="submit" class="btn btn-primary" formaction="export.php?type=CSV">Download CSV</button>
          
        </form>

      </div>

      <div class='box-container'>
        <div>
          <?php

            Table::create(array(
              "dataSource"=>$this->dataStore('excel_data'),
              "paging"=>array(
                "pageSize"=>5
              )

            ));//Table create End

          ?>
          

        </div>
KoolReport commented on Dec 11, 2019

You need to have separated action for excel generated. In your index() action, I see that you have use $report->run()->render();. This caused error for excel because content has been output by the render function. Please remove the render() function.

Sowmya commented on Dec 11, 2019

I changed my code as follows, But still same error below the table

public function index()
   	{
   		$this->load->helper('url');
   		$this->load->database('MainiDM');
   		
   		include APPPATH."reports\ExcelReport_Ex.php";
         $report = new ExcelReport_Ex;
         $report->run();//->render();
         

         $type = isset($_GET['type'])? $_GET['type'] : 'CSV';

         if($type === 'XLSX')
         {
            $report->exportToXLSX('ExcelReportSpreadsheet')->toBrowser("ExcelReport.xlsx");
            //$report->run()->render();
         }

         elseif($type === 'ODS')
         {
            $report->exportToODS('ExcelReportSpreadsheet')->toBrowser('ExcelReport.ods');
            //$report->run()->render();
         }

         elseif($type === 'CSV')
         {
            $report->exportToCSV('ExcelReportSpreadsheet')->toBrowser('ExcelReport.csv');
           // $report->run()->render();
         }

   	}
Sowmya commented on Dec 11, 2019

Still

Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\CI3\application\libraries\koolreport\core\src\KoolReport.php:455)

Filename: excel/FileHandler.php

Error....How to resolve this error

Sowmya commented on Dec 11, 2019

The downloaded Excel File shows the above error

KoolReport commented on Dec 11, 2019

Could you please try to put

require_once APPPATH."reports\ExcelReport_Ex.php";

on top of CI controller file. And also remove the "include" statement inside the action. Let see if it works.

Sowmya commented on Dec 11, 2019

Below of table it displays the error as follows

David Winterburn commented on Dec 11, 2019

Please open your exported excel file with your text editor and see if there's any text output at the beginning of the file. Let us know the result. Thanks!

Sowmya commented on Dec 11, 2019

No Output. Empty only.

But it displays correct csv file only

David Winterburn commented on Dec 11, 2019

In that case, please try the following code:

public function index()
   	{
            ob_start(); //start buffering any output

   		$this->load->helper('url');
   		$this->load->database('MainiDM');
   		
   		include APPPATH."reports\ExcelReport_Ex.php";
         $report = new ExcelReport_Ex;
         $report->run();//->render();
         

         $type = isset($_GET['type'])? $_GET['type'] : 'CSV';
        
        ob_end_clean(); //clear and end buffering

         if($type === 'XLSX')
         {
            $report->exportToXLSX('ExcelReportSpreadsheet')->toBrowser("ExcelReport.xlsx");
            //$report->run()->render();
         }

         elseif($type === 'ODS')
         {
            $report->exportToODS('ExcelReportSpreadsheet')->toBrowser('ExcelReport.ods');
            //$report->run()->render();
         }

         elseif($type === 'CSV')
         {
            $report->exportToCSV('ExcelReportSpreadsheet')->toBrowser('ExcelReport.csv');
           // $report->run()->render();
         }

   	} 

Thanks!

Sowmya commented on Dec 11, 2019

Ya, Done. Same. No change in Output. Same errors

David Winterburn commented on Dec 11, 2019

I realize you're using BigSpreadsheetExportable. This trait allows for huge excel file exporting but there's some limitation like no chart or pivot table supported. Would you please show us your php code in the excel view file?

Sowmya commented on Dec 11, 2019
<html>
<head>
|
|
</head>
<body>
  <div class="report-content">
           <form method="post">
          <button type="submit" class="btn btn-primary" formaction="<?php echo site_url();?>\Excel?type=XLSX">Download XLSX</button>
          <button type="submit" class="btn btn-primary" formaction="<?php echo site_url();?>\Excel?type=CSV">Download CSV</button> <!-- export.php?type=CSV -->
          
        </form>

      </div>

      <div class='box-container'>
        <div>
          <?php

            Table::create(array(
              "dataSource"=>$this->dataStore('excel_data'),
              "paging"=>array(
                "pageSize"=>5
              )

            ));//Table create End

          ?>
          

        </div>
        

      </div>
    
  </div>


</body>
David Winterburn commented on Dec 11, 2019

Please check documentation and example about spreadsheet template in the following links:

https://www.koolreport.com/docs/excel/export_to_big_spreadsheet/#spreadsheet-export-template

https://www.koolreport.com/examples/reports/excel/big_spreadsheet/

Your template is not in correct format so no content could be exported. It's advisable to copy the code example and modify it according to your need. Thanks!

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

None