当前位置:Gxlcms > PHP教程 > 详细介绍PHPExcel读取Excel时间的示例代码

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

时间:2021-07-01 10:21:17 帮助过:30人阅读

PHPExcel读取Excel 时间

  1. //读取Excel文件
  2. $info = pathinfo ( $path );
  3. $ext = $info ['extension'];
  4. $excelReader = null;
  5. if ( $ext == 'xls' )
  6. $excelReader = new PHPExcel_Reader_Excel5();
  7. else
  8. $excelReader = new PHPExcel_Reader_Excel2007();
  9. $phpExcelInstance = $excelReader->load($path);
  10. $sheet = $phpExcelInstance -> getSheet ( 0 );
  11. // 读取第一個工作表
  12. $highestRow = $sheet->getHighestRow(); // 取得总行数
  13. $highestColumm = $sheet->getHighestColumn(); // 取得总列数
  14. /** 循环读取每个单元格的数据 **/
  15. $arr = array ();
  16. for ( $row = 2 ; $row <= $highestRow ; $row ++ )
  17. { // 行数是以第2行开始
  18. $cell = $sheet->getCellByColumnAndRow(0, $row);
  19. $value = $cell->getValue();
  20. if (empty($create_date) || !isset($create_date))
  21. {
  22. $create_date = date("Y-m-d");
  23. }
  24. else
  25. {
  26. if ($cell->getDataType()==PHPExcel_Cell_DataType::TYPE_NUMERIC)
  27. {
  28. $cellstyleformat=$cell->getStyle()->getNumberFormat(); //获取cell类型
  29. $formatcode=$cellstyleformat->getFormatCode(); //获取cell格式代码,时间格式为mm-dd-yy
  30. if (preg_match('/^(/[/$[A-Z]*-[0-9A-F]*/])*[hmsdy]/i', $formatcode))
  31. {
  32. $create_date = gmdate("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($value));
  33. }else{
  34. $create_date = PHPExcel_Style_NumberFormat::toFormattedString($value,$formatcode);
  35. }
  36. }
  37. else
  38. {
  39. $create_date = date("Y-m-d");
  40. }
  41. }
  42. //读取Excel文件
  43. $info = pathinfo ( $path );
  44. $ext = $info ['extension'];
  45. $excelReader = null;
  46. if ( $ext == 'xls' )
  47. $excelReader = new PHPExcel_Reader_Excel5();
  48. else
  49. $excelReader = new PHPExcel_Reader_Excel2007();
  50. $phpExcelInstance = $excelReader->load($path);
  51. $sheet = $phpExcelInstance -> getSheet ( 0 );
  52. // 读取第一個工作表
  53. $highestRow = $sheet->getHighestRow(); // 取得总行数
  54. $highestColumm = $sheet->getHighestColumn(); // 取得总列数
  55. /** 循环读取每个单元格的数据 **/
  56. $arr = array ();
  57. for ( $row = 2 ; $row <= $highestRow ; $row ++ )
  58. { // 行数是以第2行开始
  59. $cell = $sheet->getCellByColumnAndRow(0, $row);
  60. $value = $cell->getValue();
  61. if (empty($create_date) || !isset($create_date))
  62. {
  63. $create_date = date("Y-m-d");
  64. }
  65. else
  66. {
  67. if ($cell->getDataType()==PHPExcel_Cell_DataType::TYPE_NUMERIC)
  68. {
  69. $cellstyleformat=$cell->getStyle()->getNumberFormat(); //获取cell类型
  70. $formatcode=$cellstyleformat->getFormatCode(); //获取cell格式代码,时间格式为mm-dd-yy
  71. if (preg_match('/^(/[/$[A-Z]*-[0-9A-F]*/])*[hmsdy]/i', $formatcode))
  72. {
  73. $create_date = gmdate("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($value));
  74. }else{
  75. $create_date = PHPExcel_Style_NumberFormat::toFormattedString($value,$formatcode);
  76. }
  77. }
  78. else
  79. {
  80. $create_date = date("Y-m-d");
  81. }
  82. }

相关文章:

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

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

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

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

人气教程排行