教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 phpexcel读取excel表格时间的例子

phpexcel读取excel表格时间的例子

发布时间:2016-11-29   编辑:jiaochengji.com
教程集为您提供phpexcel读取excel表格时间的例子等资源,欢迎您收藏本站,我们将为您提供最新的phpexcel读取excel表格时间的例子资源
phpexcel是php中专业来操作excel表格的一个php插件了,下文我们就来看看phpexcel读取excel表格时间的例子,希望下文能够帮助到各位。


编辑通过excel表格修改了大批的产品价格和促销时间,让我们技术批量导入到线上数据库。

这样对于我们来说是一件在简单不过的事情了,保护phpexcel导表利器,瞬间解决问题。

可是,进入数据库一看:蒙了,导入的时间格式有问题,展示的不是时间,是数字,郁闷中。

然后通过php输出,果然不是时间的格式。

百度一遍发现,phpexcel里面提供了这样的方法getFormattedValue()来读出时间的,将getValue()换成
getFormattedValue();

$abc = $currentSheet->getCell ( ‘A’ . $currentRow )->getFormattedValue ();

这样就可以顺利的读出excel表里的时间,重新更新数据库,OK。

下面看个例子

error_reporting(E_ALL);
date_default_timezone_set('Asia/shanghai');
/** PHPExcel_IOFactory */
require_once '../Classes/PHPExcel/IOFactory.php';
$inputFileName = '6081076641077444758.xls';
$objReader = new PHPExcel_Reader_Excel5();
$objPHPExcel = $objReader->load($inputFileName);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数

$tempArray = array();
for($j=2;$j<=$highestRow;$j ){
   for($k='A';$k<=$highestColumn;$k ){
    if($k=='M'||$k=='O') //M列和O列是时间
  $tempArray[] = excelTime($objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue());
 else
  $tempArray[] = $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue();
 }
 print_r($tempArray);
 unset($tempArray);
}


function excelTime($date, $time = false) {
 if(function_exists('GregorianToJD')){
  if (is_numeric( $date )) {
  $jd = GregorianToJD( 1, 1, 1970 );
  $gregorian = JDToGregorian( $jd intval ( $date ) - 25569 );
  $date = explode( '/', $gregorian );
  $date_str = str_pad( $date [2], 4, '0', STR_PAD_LEFT )
  ."-". str_pad( $date [0], 2, '0', STR_PAD_LEFT )
  ."-". str_pad( $date [1], 2, '0', STR_PAD_LEFT )
  . ($time ? " 00:00:00" : '');
  return $date_str;
  }
 }else{
  $date=$date>25568?$date 1:25569;
  /*There was a bug if Converting date before 1-1-1970 (tstamp 0)*/
  $ofs=(70 * 365 17 2) * 86400;
  $date = date("Y-m-d",($date * 86400) - $ofs).($time ? " 00:00:00" : '');
 }
  return $date;
}

您可能感兴趣的文章:
phpexcel导入excel到数据库的代码
phpexcel读取excel表格时间的例子
phpexcel导出数据的实例代码
解决php下载excel无法打开的问题
PHP导入与导出Excel文件的方法
PHP导出excel php使用phpexcel导出excel文件
PHP通过PHPExcel类导入导出excel
drupal读取excel并导入数据库方法
PHP导出EXCEL的简单范例 使用phpexcel类库导出excel
PHPExcel读取excel文件的例子

[关闭]
~ ~