---
title: " phpexcel读取excel时间的坑"
author: "lucklrj"
date: "2014-08-22T16:34:37Z"
tags: []
categories: ["编程语言"]
slug: "phpexcel-excel"
draft: false
lastmod: "2014-08-22T16:34:37Z"
id: 2107920
url: "https://lucklrj.iteye.com/blog/2107920"
---

按照phpexcle的api，将excle解析为数组的代码：

```java

$this->excel = PHPExcel_IOFactory::load($this->file_path);
$sheetData = $this->excel->getActiveSheet()->toArray(null,true,true,true);

```


如果字段有日期格式，比如2014-06-28 23:12:08，解析出来的数组对应的日期字段有问题，百度了下各种解决办法，以下最方便。

```java

$this->excel = PHPExcel_IOFactory::load($this->file_path);
$ActiveSheet=$this->excel->getActiveSheet();
$ActiveSheet->getStyle('G')->getNumberFormat()->setFormatCode('yyyy-mm-dd h:i');
$sheetData = $ActiveSheet->toArray(null,true,true,true);

```

如果excle文件列是动态的话，就只能循环列，内容用正则来判断。