如何在xls-phpoffice中将单元格设置为百分比



嘿,我想把xls中单元格的格式设置为百分比。我正在使用phpoffice。我想要的数值是0.04%。问题的根源$innervalue是0.04%作为字符串。这是我的代码:

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, PhpOfficePhpSpreadsheetCellDataType::TYPE_NUMERIC); //put the inner value as number
$sheet->getStyle($cell)->getNumberFormat()->setFormatCode('#.##%'); // format the cell for 0.04%
}

问题是单元格显示了4%而不是0.04%。

如果我删除线路

$sheet->getStyle($cell)->getNumberFormat()->setFormatCode('#.##%'); // format the cell for 0.04%

在xls显示0.04作为数字,但我需要它在xls中显示0.04%作为数字

这就是解决方案:

需要从此链接获取格式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(PhpOfficePhpSpreadsheetStyleNumberFormat::FORMAT_PERCENTAGE_00);
}

最新更新