详细介绍PHPExcel读取Excel时间的示例代码

 

详细介绍PHPExcel读取Excel时间的示例代码
作者: PHP中文网|标签:phpexcel Excel|2017-3-25 11:53

PHPExcel读取Excel 时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//读取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");                   
    }               
    }

相关文章:

php导出excel时科学计数法的处理方法

php原生导出excel文件的两种方法

通过php实现获取excel文档内容的代码实例

以上就是详细介绍PHPExcel读取Excel时间的示例代码的详细内容,更多请关注php中文网其它相关文章!

 

posted @ 2017-06-21 15:13  sky20080101  阅读(125)  评论(1)    收藏  举报