按列和行在PHPExcel中将单元格作为字符串 [英] Getting Cell as String in PHPExcel by column and row

查看:114
本文介绍了按列和行在PHPExcel中将单元格作为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将可能带有尾随零的单元格读取为字符串而不是数字(去掉前导零).单元格由整数列/行读取,如下所示,而不是列字符串,因为这个 answer 有.

I am trying to read a cell with possible trailing zeros as a string instead of numeric (which strips off leading zeros). The cell is read by integer column/row as below instead of column string as this answer has.

初始代码

    $instReader = $reader->load($this->file);
    $sheet = $instReader->getSheet(0);

我尝试修改它:

$keyCell = $sheet->getCellByColumnAndRow(1,5);

到:

$sheet->setCellValueExplicitByColumnAndRow(1,5, PHPExcel_Cell_DataType::TYPE_STRING);
$keyCell = $sheet->getCellByColumnAndRow(1,5);

前者为 $keyCell 提供 1407 而不是 01407

the former gives 1407 for $keyCell instead of 01407

后者给出s"或"

如何在调用 getCellByColumnAndRow 并仅对列和行使用整数值之前将单元格视为字符串.

how do I treat the cell as string before calling getCellByColumnAndRow and using only integer values for column and row.

(顺便说一句,如果可以对整个列执行一次,而不是每次对每个单独的单元格执行一次,那就更好了)

(BTW, if this can be done once for an entire column instead of each time for each individual cell that would be better)

推荐答案

$keyCell = $sheet->getCellByColumnAndRow(1,5)->getValue();

将以 Excel 实际存储的格式读取单元格数据,您不能随意更改它或告诉 PHPExcel 将其作为不同的数据类型读取.

Will read the cell data in the format that it's actually stored by Excel, you can't arbitrarily change that or tell PHPExcel to read it as a different datatype.

但是,如果单元格应用了格式,那么您可以使用

However, if the cell has formatting applied, then you can use

$keyCell = $sheet->getCellByColumnAndRow(1,5)->getFormattedValue();

相反,这将以字符串形式返回数据,并使用 Excel 电子表格中定义的任何格式掩码

instead, and this will return the data as a string, with whatever format mask was defined in the Excel spreadsheet

这篇关于按列和行在PHPExcel中将单元格作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆