Nisham Mahsin
Nisham Mahsin

Reputation: 1399

PHPExcel +Codeigniter: Invalid character

I am trying to export excel file using PHPExcel and Codeigniter as this.

I can't download the excel file but output is showing in console with an invalid character. This is the screen shot of console output.

enter image description here

Upvotes: 0

Views: 2806

Answers (2)

Jubayer Arefin
Jubayer Arefin

Reputation: 495

I have created excel files with the following code and it works. Please make sure you set the headers correctly.

    $this->load->library('Excel');

    $objPHPExcel = new PHPExcel();

    $objPHPExcel->getProperties()->setCreator("Creator name");
    $objPHPExcel->getProperties()->setLastModifiedBy("Creator name");
    $objPHPExcel->getProperties()->setTitle("File Title");
    $objPHPExcel->getProperties()->setSubject("Content Subject");
    $objPHPExcel->getProperties()->setDescription("Content Description");
    $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getActiveSheet()->setTitle('Sheet Title');
    $filename = 'sample_' . time() . '.xls'; //save our workbook as this file name
    header('Content-Type: application/vnd.ms-excel'); //mime type
    header('Content-Disposition: attachment;filename="' . $filename . '"'); //tell browser what's the file name
    header('Cache-Control: max-age=0'); //no cache
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');

    exit;

Upvotes: 2

Pixel
Pixel

Reputation: 1976

1. First solution

I had the same probleme. Use the class developped by Eliselab

https://github.com/EllisLab/CodeIgniter/wiki/Export-to-Excel-2013

For export you just need to put this on your controller.

$this->load->library('export');
$this->load->model('YourModel');

$result = $this->YourModel->YourQueryFunction();
$this->export->to_excel($result, 'nameForFile');

2. Second way

If you want to use PHPExcel try to put the same charset on all your pages, File charset & content charset. (UTF-8)

Put this on your file header :

header('Content-Type: text/html; charset=utf-8');

And change the type of file in UTF-8 Without Bom

On Nodepadd++ go to :

Click on "Format" Select "Encode in UTF-8 without BOM"

I hope it will help you.

Upvotes: 2

Related Questions