php 在 Excel 中始终显示指定的小数位数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10833031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:09:39  来源:igfitidea点击:

Always display specified number of decimal places in Excel

phpphpexcel

提问by Tiny

I need to display two decimal places in Excel (xlsx). I'm using PHPExcel. In PHP, I can use something like the following to display only the specified number of decimal paces.

我需要在 Excel (xlsx) 中显示两位小数。我正在使用PHPExcel. 在 PHP 中,我可以使用类似下面的内容来仅显示指定的小数步数。

echo sprintf("%0.2f", $row['price']);

which always displays two decimal places even though the value contains no decimal point i.e if the value of $row['price']is 1500, it will echo 1500.00. I need to write the same value to excel. I tried

它总是显示,即使值未包含小数点也就是说,如果值小数点后两位$row['price']1500,它会响应1500.00。我需要将相同的值写入excel。我试过

$sheet->setCellValue("A1", sprintf("%0.2f", $row['price']));

but it displays 1500instead of displaying 1500.00(because Excel automatically assumes it to be a numeric value and the part after the decimal point i.e .00in this case is truncated).

但它显示1500而不是显示1500.00(因为 Excel 自动假定它是一个数值并且小数点后的部分即.00在这种情况下被截断)。

I also tried the following

我也尝试了以下

$sheet->getStyle("A1")->getNumberFormat()->setFormatCode('0.00');

but it's not sufficient to achieve what is specified.

但这还不足以实现指定的目标。

How can I (always) display the specified number of decimal places in Excel using PHPExcel?

如何(始终)使用 PHPExcel 在 Excel 中显示指定的小数位数?

回答by Mark Baker

$sheet->getStyle("A1")->getNumberFormat()->setFormatCode('0.00'); 

should work, as long as the value in cell A1 is a number and not a formatted string... don't try to force Excel formatting by using sprintf, simply use the format codes.

应该可以工作,只要单元格 A1 中的值是数字而不是格式化字符串...不要尝试使用 sprintf 强制 Excel 格式化,只需使用格式代码。

回答by robert.dejesica

Formatted as number with comma. Assuming that value is not exceeded by 9 digits.

格式为带逗号的数字。假设该值不超过 9 位数字。

$sheet->getStyle("A1")->getNumberFormat()->setFormatCode('###,###,###.##');

回答by Ichigo79

You can also do this:

你也可以这样做:

$sheet->setCellValue("A1",sprintf("%0.2f",$row['price']),true)->getStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);

Adding 'true' at setCellValue, you refer directly to the cell instead of the worksheet and you can call the getStyle method without the cell value.

在 setCellValue 中添加“true”,您可以直接引用单元格而不是工作表,并且可以在没有单元格值的情况下调用 getStyle 方法。

回答by Bruno

If you want some special characters just use html_entity_decode:

如果您想要一些特殊字符,只需使用 html_entity_decode:

$currencyFormat = html_entity_decode(" 0,0.00",ENT_QUOTES,'UTF-8');
$objPHPExcel->getActiveSheet()
            ->getStyle("A1")
            ->getNumberFormat()->setFormatCode($currencyFormat);

回答by Lavanya

$objPHPExcel->getActiveSheet()->getStyle('ce')->getNumberFormat()->setFormatCode('0.00');

回答by simhumileco

Try:

尝试:

$sheet->getStyle("A1")->getNumberFormat()->setFormatCode('### ### ### ##0.00');

it works.

有用。