在创建XLSX文件并尝试将大量小数位写入数值时,我在PHPSpreadsheet上遇到了困难。
PHPSpreadsheet将我的14个小数点后的数字四舍五入,但是我需要将它们完全按所示方式存储。
我正在使用documentation中所述的setFormatCode('0.00000000000000')
,但是它没有按我期望的那样工作。
这是我的测试代码:
<?php
require __DIR__ . '/vendor/autoload.php'; // Installed via composer
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$array = [
0.03790728347833,
1345.28748532874927,
121345.18248762894914, // all 14 DP
];
$format = '0.00000000000000'; // 14 DP
// write the data
$spreadsheet->getActiveSheet()
->fromArray($array, null, 'A1');
// Format the cells
$spreadsheet->getActiveSheet()->getStyle('A1:C1')->getNumberFormat()->setFormatCode($format);
// Column sizing
foreach(range('A','C') as $columnID)
{
$spreadsheet->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
}
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save("test.xlsx");
// A1 = 0.03790728347833 - same
// A2 = 1345.28748532870000 - rounded
// A3 = 121345.18248763000000 - rounded
有人可以提供一种无需舍入就可以存储这么多小数位的方法吗?