Home > php教程 > php手册 > phpexcel读取excel表格时间的例子

phpexcel读取excel表格时间的例子

WBOY
Release: 2016-05-26 08:19:49
Original
2567 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。

下面看个例子

<?php
error_reporting(E_ALL);
date_default_timezone_set(&#39;Asia/shanghai&#39;);
/** PHPExcel_IOFactory */
require_once &#39;../Classes/PHPExcel/IOFactory.php&#39;;
$inputFileName = &#39;6081076641077444758.xls&#39;;
$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 = &#39;A&#39;; $k <= $highestColumn; $k++) {
        if ($k == &#39;M&#39; || $k == &#39;O&#39;) //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(&#39;GregorianToJD&#39;)) {
        if (is_numeric($date)) {
            $jd = GregorianToJD(1, 1, 1970);
            $gregorian = JDToGregorian($jd + intval($date) - 25569);
            $date = explode(&#39;/&#39;, $gregorian);
            $date_str = str_pad($date[2], 4, &#39;0&#39;, STR_PAD_LEFT) . "-" . str_pad($date[0], 2, &#39;0&#39;, STR_PAD_LEFT) . "-" . str_pad($date[1], 2, &#39;0&#39;, STR_PAD_LEFT) . ($time ? " 00:00:00" : &#39;&#39;);
            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" : &#39;&#39;);
    }
    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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template