PHPExcel,从excel文件中获取长文本作为文本

时间:2015-03-10 07:01:07

标签: php phpexcel

我正在尝试阅读包含电话号码的Excel文件。读取该单元格时,它将其作为数字而不是文本返回。因此,我没有得到电话号码0012312345678,而是将其作为12312345000 电话号码都在第一栏(A)中。

我读取excel文件的代码是:

$retArray = array();
$Reader = PHPExcel_IOFactory::createReaderForFile($file);
$Reader->setReadDataOnly(true);
$objXLS = $Reader->load($file);
$objWorksheet = $objXLS->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
$highestCol = $objWorksheet->getHighestColumn();
$highestCol = PHPExcel_Cell::columnIndexFromString($highestCol);
$cnt = 0;
$range = "A:A";
for ($row = 1; $row <= $highestRow; $row++) {
    for ($col = 0; $col <= $highestCol-1; $col++){
        $objWorksheet->getStyle($range)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_TEXT );
        $retArray[$row][$col] = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
    }
    $cnt++;
}
$objXLS->disconnectWorksheets();
unset($objXLS);
echo "$cnt" . "|" . json_encode($retArray);

2 个答案:

答案 0 :(得分:7)

在php文件的顶部粘贴:

ini_set('precision', '15');

这套解决了我的问题:

返回:3.5410705883463E + 14 真实价值:357888057644723

答案 1 :(得分:0)

$totalRows = $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
$objPHPExcel->getActiveSheet()
            ->getStyle('A1:A' . $totalRows)
            ->getNumberFormat()
            ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);

May be help!