PhpSpreadsheet导出EXCEL长数字为科学计数法的解决办法

博客作者在使用PhpSpreadsheet导出Excel时遇到一个问题,即身份证号码被错误地以科学计数法格式显示。经过搜索和尝试,作者发现可以使用`setCellValueExplicitByColumnAndRow()`方法并指定`DataType::TYPE_STRING`来将单元格格式设置为文本,从而正确导出身份证号。这是一个避免修改插件源码的解决方案。

今天发现PhpSpreadsheet导出EXCEL有个小坑,就是在导出身份证号的时候,导出来的EXCEL单元格使用的是科学计数法的格式(数字格式)。如下图所示:

网上查了一下,我混乱了,因为说什么的都有。有的说加“NumberFormat::FORMAT_NUMBER”,有的去修改插件源码。我真的是有点醉了。按照平时使用EXCEL的习惯,不是直接把单元格格式设置成“文本”格式吗?难道PhpSpreadsheet就没有这一功能吗?

在我继续搜索之下,还真是有这个功能的。

 

setCellValueExplicitByColumnAndRow() 

Worksheet.php : 1211

Set a cell value by using numeric cell coordinates.

public setCellValueExplicitByColumnAndRow(int $columnIndex, int $row, mixed $value, string $dataType) : $this

Parameters

$columnIndex : int

Numeric column coordinate of the cell

$row : int

Numeric row coordinate of the cell

$value : mixed

Value of the cell

$dataType : string

Explicit data type, see DataType::TYPE_* Note that PhpSpreadsheet does not validate that the value and datatype are consistent, in using this method, then it is your responsibility as an end-user developer to validate that the value and the datatype match. If you do mismatch value and datatpe, then the value you enter may be changed to match the datatype that you specify.

Tags 

Deprecated

1.23.0 Use the setCellValueExplicit() method with a cell address such as 'C5' instead;, or passing in an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.

Return values

$this —

 实际使用:

 $sheet->setCellValueExplicitByColumnAndRow(//横坐标//, //纵坐标//, //单元格的值//,DataType::TYPE_STRING); //※修改为以坐标获取单元格的方式赋值,“DataType::TYPE_STRING”为设置为文本格式

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值