I have a multi-line cell value that I am trying to insert into an Excel sheet. While using multiline value, the cell values are duplicated every 16 rows.
For Example
- IF there are a total of 16 rows. Every cell value will print properly ( https://prnt.sc/wPKKpNxthVl5 ).
- The total row is greater than 16 and less than or equal to 32. Then the cell value is printed times for rows 1 to 16 ( https://prnt.sc/lVmuiQ0OwqRI ), while other rows will have a single value ( https://prnt.sc/Xm5IJShAw3SJ ).
- There are more rows than 32 and less than or equal to 48. Then the cell value is printed 3 times for rows 1 to 16 ( https://prnt.sc/POWVZK1Yvncy ), 2 times for rows 17 to 32 ( https://prnt.sc/anTsJWomhdCv ), and 1 time for other rows.
- So on...
<?php
require __DIR__ . '/vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\IOFactory;
$spreadSheetObject = new Spreadsheet();
$range = range(1, 34);
foreach ($range as $row) {
$spreadSheetObject->getActiveSheet()->setCellValue("A" . $row, "- Line1 \r\n - Line2");
}
$fileName = time().'.xlsx';
$writer = IOFactory::createWriter($spreadSheetObject, 'Xlsx');
$writer->save($fileName);
Used versions of the code:
- PHP: 8.1
- phpoffice/phpspreadsheet: ^1.29
Note
- Issue only facing for new line like
- Line1 \r\n - Line2
- For single line value working fine