我试图读取一个单元格,其中可能有尾随零作为字符串而不是数字(剥离前导零).正如此答案所示,单元格由整数列/行读取,而不是列字符串.
初始代码
$instReader = $reader->load($this->file);
$sheet = $instReader->getSheet(0);
Run Code Online (Sandbox Code Playgroud)
我试过修改这个:
$keyCell = $sheet->getCellByColumnAndRow(1,5);
Run Code Online (Sandbox Code Playgroud)
至:
$sheet->setCellValueExplicitByColumnAndRow(1,5, PHPExcel_Cell_DataType::TYPE_STRING);
$keyCell = $sheet->getCellByColumnAndRow(1,5);
Run Code Online (Sandbox Code Playgroud)
前者为$ keyCell而不是01407提供1407
后者给出"s"或""
在调用getCellByColumnAndRow并仅使用列和行的整数值之前,如何将单元格视为字符串.
(顺便说一句,如果这可以针对整个列进行一次,而不是每次针对每个单独的单元格做得更好)
$keyCell = $sheet->getCellByColumnAndRow(1,5)->getValue();
Run Code Online (Sandbox Code Playgroud)
将以Excel实际存储的格式读取单元格数据,您不能随意更改它或告诉PHPExcel将其作为不同的数据类型读取.
但是,如果单元格已应用格式,则可以使用
$keyCell = $sheet->getCellByColumnAndRow(1,5)->getFormattedValue();
Run Code Online (Sandbox Code Playgroud)
相反,这将以字符串形式返回数据,使用Excel电子表格中定义的任何格式掩码