PHPExcel Accounting Formats

只愿长相守 提交于 2019-12-14 03:43:41

问题


I'm working with PHPExcel and I'm trying to format a cell using Excel's built-in "Accounting" format. I'm aware of the Format Code:

PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE

But this simply formats to two decimal places and adds a $ in front of the number. The result I'm looking for is the right aligned cell with the $ on the left. $0 values should be listed as "-" and negative values should be $ (1.11)

As far as I can tell there are no other currency formats in the documentation (though I may have missed it, this documentation is horrendous). Am I looking in the wrong place? Can this be achieved with regular cell formatting or is Excel doing something unique for Accounting?


回答1:


I reverse engineered the format code from an existing spreadsheet using PHPExcel and got this:

_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)

This is the code Excel places on the cell when you select the "Accounting" format... or click that "$" toolbar button in Excel 2007.




回答2:


$objPHPExcel->getActiveSheet()->getStyle('C1')->getNumberFormat()->setFormatCode("#,##0.00");

or use

$objPHPExcel->getActiveSheet()->getStyle('C1')->getNumberFormat()->setFormatCode("#.##0,00");

Set in getStyle CELL.




回答3:


There are no other pre-defined formats beyond those listed in PHPExcel_Style_NumberFormat, but you should be able to set the format code to any string that you could use when setting an MS Excel custom format...

e.g.

[green]$#,##0.00;[red]$(-#,##0.00)

As regards cell alignment, set this to right yourself, or don't set it at all.



来源:https://stackoverflow.com/questions/5669941/phpexcel-accounting-formats

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!