简体   繁体   中英

PHPExcel save error - Cell coordinate string can not be a range of cells

I am currently trying to save an excel file loaded with PHPExcel. The template file has multiple sheets that reference other sheets in formulas. The template is being loaded from my local Apache server. After loading the file I try to save it and it throws an exception "Cell coordinate string can not be a range of cells". Using the HTML writer it displays perfectly.

Here is my code:

require_once "assets/vendors/PHPExcel/PHPExcel.php";
require_once 'assets/vendors/PHPExcel/PHPExcel/IOFactory.php';
$tmpfname = 'template_files/templateFile.xlsx';

$objReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
$objPHPExcel = $objReader->load($tmpfname);

try {
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    header('Content-Disposition: attachment;filename="myfile.xlsx"');
    header('Cache-Control: max-age=0');
    $objWriter->save('php://output');  

} catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
}

I expected my template sheet to be downloaded as usual in PHPExcel but cannot solve this issue. Has anyone else run into something similar?

The document that is downloaded is completely blank with the exception printed "Caught exception: Cell coordinate string can not be a range of cells".

For anyone that runs into this exception in the future:

Remove the formatting from your document and try saving it with PHPExcel. I am not sure of the exact source, but after removal I replicated the formatting with Google Sheets and the writer exported perfectly fine.

What's in the PHP output?

You have bad data somewhere. This exception is thrown under three cases:

  1. An attempt to find an absoluteCoordinate is passed a string containing ":" or ","

OR

  1. An attempt to find an absoluteReference is passed a string containing ":" or ","

OR

  1. coordinateFromString is called with ":" or "," in the string variable.

I'd look for a stray comma or full-colon where it's not supposed to be in the PHP output or a cell within your template file ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM