[英]How to set cell as percentage in xls - phpoffice
Hey all I want to set the format of a cell in xls to percentage.嘿,我想将 xls 中单元格的格式设置为百分比。 I am using phpoffice.
我正在使用 phpoffice。 the value that I want is 0.04% as percentage.
我想要的值是 0.04% 的百分比。 the problem the origin $innervalue is 0.04% as string.
原点 $innervalue 的问题是 0.04% 作为字符串。 this is my code:
这是我的代码:
if($column == 'E')
{
$innervalue = str_replace('%','',$innervalue); //remove the % sign for float
$innervalue = (float)$innervalue; // set innervalue as float and not string
$sheet->setCellValueExplicit($cell, $innervalue, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NUMERIC); //put the inner value as number
$sheet->getStyle($cell)->getNumberFormat()->setFormatCode('#.##%'); // format the cell for 0.04%
}
the problem is that the cell displayed 4.% instead of 0.04%.问题是单元格显示 4.% 而不是 0.04%。
if I remove the line如果我删除该行
$sheet->getStyle($cell)->getNumberFormat()->setFormatCode('#.##%'); // format the cell for 0.04%
in the xls displayed 0.04 as number, but I need it to display in the xls 0.04% as number在 xls 中显示 0.04 作为数字,但我需要它在 xls 中显示 0.04% 作为数字
This is the solution:这是解决方案:
Need to get the format from this link https://github.com/PHPOffice/PhpSpreadsheet/blob/master/src/PhpSpreadsheet/Style/NumberFormat.php需要从此链接获取格式https://github.com/PHPOffice/PhpSpreadsheet/blob/master/src/PhpSpreadsheet/Style/NumberFormat.php
if($column == 'E')
{
$sheet->setCellValue($cell, $innervalue); //put the inner value as number
$sheet->getStyle($cell)->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_PERCENTAGE_00);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.