dongpa5277 2018-02-28 15:48
浏览 299

在PHP Excel中添加百分比数字格式

When I set format of a cell as %, it automatically multiplies the value with 100 & shows the value. So 94 becomes 9400%.

I want to apply % format on a column but want to keep the values same as before. I just want % sign to be shown along with the value. My Value should be 94% . To achieve this, I used following format code

$this->_xls_current_sheet->getStyle($cell_coordinates)->getNumberFormat()->applyFromArray( 
 array( 
 'code' => PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE
));
  • 写回答

1条回答 默认 最新

  • doushi4864 2018-02-28 19:28
    关注

    If you store 0.5 in a cell, and format it as a percentage, then it will appear as 50%.... if you store 50 in a cell, and format it as a percentage, then it will appear as 5000% that is how it should be.

    If you enter 50 as a number in the MS Excel GUI, and then set the format mask manually to a percentage, you will get a displayed value of 5000%. However, if you enter 50% in the MS Excel GUI, then MS Excel divides the value by 100 to store 0.5 in the cell, and formats it as a percentage (two actions in one step). If you set 50% in this way in MS Excel, and then change the format mask from percentage to number, then you will see that MS Excel has done this division for you.

    PHPExcel doesn't do this (by default)... it expects you to set the correct value and the format as two separate steps. So PHPExcel expects you to store the correct value in the cell (0.5) in the first place, when you want to format it as a percentage.... it will not magically change the value in any way, it doesn't multiply it at all.

    Note that if you are using the advanced value binder (rather than the default value binder), and set the cell value to 50%, then PHPExcel will divide by 100 and set to a percentage automatically, in the same way as the MS Excel GUI.

    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害