PhpSpreadsheet icon indicating copy to clipboard operation
PhpSpreadsheet copied to clipboard

Exception 'PhpOffice\PhpSpreadsheet\Exception' with message 'Sheet does not exist.'

Open xjcallen opened this issue 1 year ago • 1 comments

$excel = IOFactory::load("abc.xlsx"); $sheet1 = clone $excel->getSheet(0); $sheet1->setCellValue('A1','1111111111111'); $excel->addSheet($sheet1);

企业微信截图_20241016200007

xjcallen avatar Oct 16 '24 12:10 xjcallen

You need perform your addSheet before setCellValue. (You also need to change the sheet title first; I assume you just omitted that from your code fragment since you would have seen a different error if you didn't do that.)

This is a fairly unusual situation - it will happen only with cloned worksheets. It might be possible to make the PhpSpreadsheet code more resilient; that would add overhead to all setCellValue calls, and lose some functionality (setting quotePrefix attribute correctly) when it is needed. I'll have to think about whether it's worthwhile. In the meantime, you can re-code as above.

oleibman avatar Oct 16 '24 15:10 oleibman