Home > Backend Development > PHP Tutorial > Example of using PHPExcel to identify and format date format in Excel

Example of using PHPExcel to identify and format date format in Excel

WBOY
Release: 2016-07-25 09:05:33
Original
1529 people have browsed it
  1. function Read_Excel_File2($file_name,&$result){
  2. require_once 'include/PHPExcel/Classes/PHPExcel/IOFactory.php';
  3. $result=null;
  4. $objReader = PHPExcel_IOFactory::createReader('Excel5');
  5. // $objReader->setReadDataOnly(true);
  6. try{
  7. $objPHPExcel = $objReader->load($file_name);
  8. }catch(Exception $e){}
  9. if(!isset($objPHPExcel)) return "无法解析文件";
  10. $allobjWorksheets = $objPHPExcel->getAllSheets();
  11. foreach($allobjWorksheets as $objWorksheet){
  12. $sheetname=$objWorksheet->getTitle();
  13. $highestRow = $objWorksheet->getHighestRow(); // e.g. 10
  14. $highestColumn = $objWorksheet->getHighestColumn();
  15. $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
  16. for ($row = 1; $row <= $highestRow; ++$row) {
  17. for ($col = 0; $col <= $highestColumnIndex; ++$col) {
  18. $cell =$objWorksheet->getCellByColumnAndRow($col, $row);
  19. $value=$cell->getValue();
  20. if($cell->getDataType()==PHPExcel_Cell_DataType::TYPE_NUMERIC){
  21. $cellstyleformat=$cell->getParent()->getStyle( $cell->getCoordinate() )->getNumberFormat();
  22. $formatcode=$cellstyleformat->getFormatCode();
  23. if (preg_match('/^([$[A-Z]*-[0-9A-F]*])*[hmsdy]/i', $formatcode)) {
  24. $value=gmdate("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($value));
  25. }else{
  26. $value=PHPExcel_Style_NumberFormat::toFormattedString($value,$formatcode);
  27. }
  28. // echo $value,$formatcode,'
    ';
  29. }
  30. $result[$sheetname][$row-1][$col]=$value;
  31. }
  32. }
  33. }
  34. return 0;
  35. }
复制代码

其中,关于日期判断的部分主要是以下部分:

  1. $cell =$objWorksheet->getCellByColumnAndRow($col, $row);
  2. $value=$cell->getValue();
  3. if($cell->getDataType()==PHPExcel_Cell_DataType::TYPE_NUMERIC){
  4. $cellstyleformat=$cell->getParent()->getStyle( $cell->getCoordinate() )->getNumberFormat();
  5. $formatcode=$cellstyleformat->getFormatCode();
  6. if (preg_match('/^([$[A-Z]*-[0-9A-F]*])*[hmsdy]/i', $formatcode)) {
  7. $value=gmdate("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($value));
  8. }else{
  9. $value=PHPExcel_Style_NumberFormat::toFormattedString($value,$formatcode);
  10. }
  11. }
复制代码

以上用到的PHPExcel版本是 1.7.2 。

您可能感兴趣的文章: PHPExcel常用方法举例 PHP导出EXCEL的简单范例 使用phpexcel类库导出excel phpExcel类的使用方法分享 phpexcel导出excel的经典实例 PHPExcel读取excel文件的例子 phpexcel类库实例 支持(excel2003 excel2007) phpexcel导出数据的实例代码 phpexcel导入excel到数据库的代码 phpexcel快速开发指南(不错) phpExcel中文帮助手册(知识点) phpexcel导出excel的颜色与网页中颜色不一致的解决方法 CI中使用PHPExcel导出数据到Excel



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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template