some chart losts on multiple sheets and excel file corrupt

Topics: Developer Forum, Project Management Forum, User Forum
Sep 7, 2012 at 5:54 AM
Edited Sep 7, 2012 at 6:06 AM

Hello

I want to create charts with existing template excel file in my web server.
This template file has many sheets. almost sheets have charts and empty data tables.
In my program, the data from DB are plotted data tables and output into the directory in server.
So, I expect that the chart has been drawn on the sheet comes out.
But, some charts are lost. this is my problem.

my code is this:

public function write($filename, $savePath, array $data, $fileType = 'Excel2007'){
    App::import('Vendor', 'PHPExcel/component');

    $reader = PHPExcel_IOFactory::createReader($fileType);
    if ($fileType === 'Excel2007') $reader->setIncludeCharts(TRUE);
    try {
        if (!is_writable($filename)) {
            throw new Exception('File does not exits or is not writeable');
        }
        $phpExcel = $reader->load($filename);
    } catch(Exception $e) {
        die($e->getMessage() . " {$filename}" . PHP_EOL);
    }

/**
 * $data is like this...
 * array(
 *  'sheetNameA' => array(
 *      [0] => (col, row, value),
 *      ....
 *  ),
 *  'sheetNameB' => array(
 *      [0] => (col, row, value),
 *      ....
 *  ),
 * );
 */
    foreach ($data as $sheetName => $cell){
        $phpExcel->setActiveSheetIndexByName($sheetName);
        foreach($cell as $d){
            $phpExcel->getActiveSheet()->setCellValueByColumnAndRow($d[0], $d[1], $d[2]);
        }
    }

    $writer = PHPExcel_IOFactory::createWriter($phpExcel, $fileType);
    if ($fileType === 'Excel2007') $writer->setIncludeCharts(TRUE);
    $writer->save($savePath);
    $phpExcel->disconnectWorksheets();
    unset($phpExcel);

    return $savePath;
}

my environment :

    PHP: ver5.4.5
    PHPExcel: 1.7.7
    OS: CentOS release 5.5 (Final)

I apologize ugliy code and poor my english.
Please help me.

Sep 7, 2012 at 6:00 AM
Edited Sep 7, 2012 at 6:00 AM

I uploaded using template excel file: http://www5.gigafile.nu/v3/?341baf65365d24882f16cbdd6ce6f6f7