PHPOffice / PhpSpreadsheet

A pure PHP library for reading and writing spreadsheet files
https://phpspreadsheet.readthedocs.io
MIT License
13.26k stars 3.42k forks source link

Invalid cell coordinate TABLEAU1[#ALL] #3618

Open ThierryHFR opened 1 year ago

ThierryHFR commented 1 year ago

This is:

- [ x] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

Modify the Xlsx file supplied as input.

What is the current behavior?

I get this error: Invalid cell coordinate TABLEAU1[#ALL]

What are the steps to reproduce?

1) I access the export.php file 2) I supply an XLSX file 3) I open the file 4) I save the file as XLSX 5) Download file

Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server:

<?php
require_once dirname(__FILE__).'/vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;

$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load( "myfile.xlsx" );
$sheet = $spreadsheet->getSheet(0);
// $sheet->setCellValueByColumnAndRow(5, 2, 1);

$filename = 'yourspreadsheet.xlsx';
try {
            $writer = PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
            $writer->save($filename);
            $content = file_get_contents($filename);
} catch(Exception $e) {
             exit($e->getMessage());
}
header("Content-Disposition: attachment; filename=".$filename);
unlink($filename);
?>

If this is an issue with reading a specific spreadsheet file, then it may be appropriate to provide a sample file that demonstrates the problem; but please keep it as small as possible, and sanitize any confidential information before uploading.

What features do you think are causing the issue

Does an issue affect all spreadsheet file formats? If not, which formats are affected?

Which versions of PhpSpreadsheet and PHP are affected?

"phpoffice/phpspreadsheet": "^1.29" PHP 8.1.16

ThierryHFR commented 1 year ago

In my real situation, I import the file and all its sheets into MySql (it works perfectly). After modification (using a form), I export it, updating the cells that have been modified. :

<?php

function _upload_cg($files, $pl) {
     if (isset($files['cg']) && !empty($files['cg']['name'])) {
          // Where the file is going to be stored
          $target_dir = "data/";
          $file = $files['cg']['name'];
          $path = pathinfo($file);
          $filename = $path['filename'];
          $ext = $path['extension'];
          $temp_name = $files['cg']['tmp_name'];
          $path_filename_ext = $target_dir.$pl.'.'.$ext;
          move_uploaded_file($temp_name,$path_filename_ext);
     }
}

require_once dirname(__FILE__).'/vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;

if (isset($_POST['btn-save'])) {
        _upload_cg($_FILES, date('Ymd'));

        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load( "data/".date('Ymd').".xlsx" );
        $sheet = $spreadsheet->getSheet(0);
        // $sheet->setCellValueByColumnAndRow(5, 2, 1);

        $filename = 'yourspreadsheet.xlsx';
        try {
            $writer = PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
            $writer->save($filename);
            $content = file_get_contents($filename);
        } catch(Exception $e) {
             exit($e->getMessage());
        }
        header("Content-Disposition: attachment; filename=".$filename);
        unlink($filename);
} else {
?>
<!doctype html>
<html  lang='fr' dir='ltr'>
<head>
<title>Import Xlsx</title>
       <meta charset="utf-8">
       <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<form id="w0"  method="post" enctype="multipart/form-data">
    <div class="frmSearch">
                <div class="form-group field">
                   <label class="control-label" for="cg">Files (File XLSX only)</label>
                   <input type="file" id="cg" class="form-control" name="cg" accept=".xlsx" maxlength="4096" />
                   <div class="help-block"></div>
                </div>
                <br>
                <div class="form-group">
                   <p>
                      <input type="submit" class="btn btn-success" id="btn-save" name="btn-save" value="Import" />
                   </p>
               </div>
    </div>
</form>
</body>
</html>
<?php
}
?>