mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-23 21:28:26 +00:00
6edc552013
Fix #642. Opened over 5 years ago, probably the oldest problem I've worked on. And https://github.com/PHPOffice/PHPExcel/issues/1320, opened a year before that. And https://github.com/SpartnerNL/Laravel-Excel/issues/1521. Shared/StringHelper::UTF8toBIFF8UnicodeLong calculates incorrect length for strings when they contain characters outside Unicode BMP. Xls uses UTF-16 to encode its strings, and characters outside BMP require a surrogate pair to encode. PhpSpreadsheet (and PhpExcel before it) have been counting these as a single character, but Excel counts them as 2. Change to compute the length as half the number of bytes in the UTF-16 string, as Excel does. A formal test is added, but it's a bit difficult to follow. So I aso added a non-BMP emoji to 27template.xls, which will cause it to be both read by Xls reader and written by Xls writer. This would previously have created a corrupt worksheet. The emoji is now handled correctly.
14 lines
321 B
PHP
14 lines
321 B
PHP
<?php
|
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
|
|
require __DIR__ . '/../Header.php';
|
|
|
|
// Read from Xls (.xls) template
|
|
$helper->log('Load Xls template file');
|
|
$reader = IOFactory::createReader('Xls');
|
|
$spreadsheet = $reader->load(__DIR__ . '/../templates/27template.xls');
|
|
|
|
// Save
|
|
$helper->write($spreadsheet, __FILE__);
|