使用 PHPExcel lib 读取 xls 文件并获取单元格类型


Read xls file using PHPExcel lib and get cell type

我用PHPExcel读取XLS文件。有单元格日期列。该数据列有两种类型的单元格(日期和文本类型单元格)。

我需要获取XLS文件中单元格的视图值。因此,在获取单元格的值之前,我想确定该单元格是日期单元格还是文本单元格。

如何获取XLS文件单元的视图值。是否有像getCellType(),getViewValueOfCell()或任何其他方法来获取单元格的视图值的方法?

注意:$cell->getType不是 PHPExcel 中真正的方法。 这个伪方法。

请建议波纹管单元格读取的最佳逻辑/方法。

 // read view value form date column
 if($cell->getType == 'Date'){
  $array_data[$rowIndex][$cell->getColumn()] = PHPExcel_Style_NumberFormat::toFormattedString($cell->getCalculatedValue(), 'YYYY-MM-DD'); 
 }
 else if($cell->getType == 'Text'){
  $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
 }

这是我所有的XLS读取功能

       $objReader = new PHPExcel_Reader_Excel5();
   $objReader->setReadDataOnly(true);
   $objPHPExcel = $objReader->load($file_path);
   $rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
   $array_data = array();
   foreach ($rowIterator as $row) {
      $cellIterator = $row->getCellIterator();
      $cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
      // if(1 == $row->getRowIndex ()) continue;//skip first row
      $rowIndex = $row->getRowIndex();
      $array_data[$rowIndex] = array('A' => '', 'B' => '', 'C' => '', 'D' => '');
      foreach ($cellIterator as $cell) {
         if ('A' == $cell->getColumn()) {
            $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
         } else if ('B' == $cell->getColumn()) {
            $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
         } else if ('C' == $cell->getColumn()) {
            // read view value form date column
             if($cell->getType == 'Date'){
              $array_data[$rowIndex][$cell->getColumn()] = PHPExcel_Style_NumberFormat::toFormattedString($cell->getCalculatedValue(), 'YYYY-MM-DD'); 
             }
             else if($cell->getType == 'Text'){
              $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
             }
         } else if ('D' == $cell->getColumn()) {
            $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
         }
      }
   }

单元格的 getDataType() 将返回单元格中包含的值的数据类型

有效数据类型定义如下PHPExcel_Cell_DataType:

const TYPE_STRING2  = 'str';
const TYPE_STRING   = 's';
const TYPE_FORMULA  = 'f';
const TYPE_NUMERIC  = 'n';
const TYPE_BOOL     = 'b';
const TYPE_NULL     = 'null';
const TYPE_INLINE   = 'inlineStr';
const TYPE_ERROR    = 'e';

请注意,日期或时间没有数据类型:日期/时间是 MS Excel 中的浮点数据类型。

要确定单元格是否包含日期/时间值,您需要检查数字格式掩码。为了简化这一点,PHPExcel_Shared_Date类中提供了以下方法,用于将数字格式掩码标识为与日期/时间相关的掩码

isDateTime()

/**
 * Is a given cell a date/time?
 *
 * @param    PHPExcel_Cell  $pCell
 * @return   boolean
 */

isDateTimeFormat()

/**
 * Is a given number format a date/time?
 *
 * @param    PHPExcel_Style_NumberFormat    $pFormat
 * @return   boolean
 */

isDateTimeFormatCode()

/**
 * Is a given number format code a date/time?
 *
 * @param    string $pFormatCode
 * @return   boolean
 */

API 文档应该已为您标识了这些内容