php获取excel时间,详细介绍PHPExcel读取Excel时间的示例代码

PHPExcel读取Excel 时间//读取Excel文件

$info = pathinfo ( $path );

$ext = $info ['extension'];

$excelReader = null;

if ( $ext == 'xls' )

$excelReader = new PHPExcel_Reader_Excel5();

else

$excelReader = new PHPExcel_Reader_Excel2007();

$phpExcelInstance = $excelReader->load($path);

$sheet = $phpExcelInstance -> getSheet ( 0 );

// 读取第一個工作表

$highestRow = $sheet->getHighestRow(); // 取得总行数

$highestColumm = $sheet->getHighestColumn(); // 取得总列数

/** 循环读取每个单元格的数据 **/

$arr = array ();

for ( $row = 2 ; $row <= $highestRow ; $row ++ )

{ // 行数是以第2行开始

$cell = $sheet->getCellByColumnAndRow(0, $row);

$value = $cell->getValue();

if (empty($create_date) || !isset($create_date))

{

$create_date = date("Y-m-d");

}

else

{

if ($cell->getDataType()==PHPExcel_Cell_DataType::TYPE_NUMERIC)

{

$cellstyleformat=$cell->getStyle()->getNumberFormat(); //获取cell类型

$formatcode=$cellstyleformat->getFormatCode(); //获取cell格式代码,时间格式为mm-dd-yy

if (preg_match('/^(/[/$[A-Z]*-[0-9A-F]*/])*[hmsdy]/i', $formatcode))

{

$create_date = gmdate("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($value));

}else{

$create_date = PHPExcel_Style_NumberFormat::toFormattedString($value,$formatcode);

}

}

else

{

$create_date = date("Y-m-d");

}

}

//读取Excel文件

$info = pathinfo ( $path );

$ext = $info ['extension'];

$excelReader = null;

if ( $ext == 'xls' )

$excelReader = new PHPExcel_Reader_Excel5();

else

$excelReader = new PHPExcel_Reader_Excel2007();

$phpExcelInstance = $excelReader->load($path);

$sheet = $phpExcelInstance -> getSheet ( 0 );

// 读取第一個工作表

$highestRow = $sheet->getHighestRow(); // 取得总行数

$highestColumm = $sheet->getHighestColumn(); // 取得总列数

/** 循环读取每个单元格的数据 **/

$arr = array ();

for ( $row = 2 ; $row <= $highestRow ; $row ++ )

{ // 行数是以第2行开始

$cell = $sheet->getCellByColumnAndRow(0, $row);

$value = $cell->getValue();

if (empty($create_date) || !isset($create_date))

{

$create_date = date("Y-m-d");

}

else

{

if ($cell->getDataType()==PHPExcel_Cell_DataType::TYPE_NUMERIC)

{

$cellstyleformat=$cell->getStyle()->getNumberFormat(); //获取cell类型

$formatcode=$cellstyleformat->getFormatCode(); //获取cell格式代码,时间格式为mm-dd-yy

if (preg_match('/^(/[/$[A-Z]*-[0-9A-F]*/])*[hmsdy]/i', $formatcode))

{

$create_date = gmdate("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($value));

}else{

$create_date = PHPExcel_Style_NumberFormat::toFormattedString($value,$formatcode);

}

}

else

{

$create_date = date("Y-m-d");

}

}

相关文章:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值