今天发现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”为设置为文本格式