0

I'm developing a site using YII, PHP.

I've been using PHPexcel in some part of my web and it works just fine. In most cases the file created by phpexcel can be opened by libreoffice. But in one controller it generate a fine xls file. The file can be downloaded. But everytime I open the file from libreoffice it shows text import. Then the file shows the actual xml file in the libreoffice calc. What could be the problem?

I've tried to copy and paste the code from the working controller (another controller), but it still produces the same file (open as plain xml).

Can it be fixed from PHP or is it a bug from libreoffice? The error file can be opened successfully in MS.excel.

Thanks for any answers.

These are the xml format generated. It looks the same from the working one.

<?xml version="1.0" encoding="UTF-8"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="generatedName">

This is my code.

$data = array();
        $data = array(
            1 => array ('Report Anomali Price'),
        );
        $data = array_merge($data, array(
                array('Item Code','Name', 'Item Price','Unit Code','Location Apotek','Item Price Gondo', 'Unit Code Gondo', 'Percentage Price', 'Date'),
        ));

        $data = array_merge($data, array(
            array(''),
            array('Export By : ' .Yii::app()->user->name .' At : ' .date("d/m/Y H:i:s") .'  -  Copyright ' .date('Y') ),
        ));

        Yii::import('application.extensions.phpexcel.JPhpExcel');
        $title = "AnomaliPriceReport_".date('dmY');
        $xls = new JPhpExcel('UTF-8', false, $title);
        $xls->addArray($data);
        $xls->generateXML($title);

1 Answers1

0

I have fixed the problem. I add this before generating the excel file.

ob_end_clean();
ob_start();

Hope that it would be useful for someone with the same problem.