Home> php教程> php手册> body text

phpexcel读取excel表格时间的例子

WBOY
Release: 2016-05-26 08:19:49
Original
2464 people have browsed it

phpexcel是php中专业来操作excel表格的一个php插件了,下文我们就来看看phpexcel读取excel表格时间的例子,希望下文能够帮助到各位。

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

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

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

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

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

getFormattedValue();

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

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

下面看个例子

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; }
Copy after login


本文链接:

收藏随意^^请保留教程地址.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!