-
- function Read_Excel_File2($file_name,&$result){
- require_once 'include/PHPExcel/Classes/PHPExcel/IOFactory.php';
- $result=null;
- $objReader = PHPExcel_IOFactory::createReader('Excel5');
- // $objReader->setReadDataOnly(true);
- try{
- $objPHPExcel = $objReader->load($file_name);
- }catch(Exception $e){}
- if(!isset($objPHPExcel)) return "无法解析文件";
- $allobjWorksheets = $objPHPExcel->getAllSheets();
- foreach($allobjWorksheets as $objWorksheet){
- $sheetname=$objWorksheet->getTitle();
- $highestRow = $objWorksheet->getHighestRow(); // e.g. 10
- $highestColumn = $objWorksheet->getHighestColumn();
- $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
- for ($row = 1; $row for ($col = 0; $col $cell =$objWorksheet->getCellByColumnAndRow($col, $row);
- $value=$cell->getValue();
- if($cell->getDataType()==PHPExcel_Cell_DataType::TYPE_NUMERIC){
- $cellstyleformat=$cell->getParent()->getStyle( $cell->getCoordinate() )->getNumberFormat();
- $formatcode=$cellstyleformat->getFormatCode();
- if (preg_match('/^(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy]/i', $formatcode)) {
- $value=gmdate("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($value));
- }else{
- $value=PHPExcel_Style_NumberFormat::toFormattedString($value,$formatcode);
- }
- // echo $value,$formatcode,'
';
-
- }
- $result[$sheetname][$row-1][$col]=$value;
- }
- }
- }
- return 0;
- }
复制代码
其中,关于日期判断的部分主要是以下部分:
-
- $cell =$objWorksheet->getCellByColumnAndRow($col, $row);
- $value=$cell->getValue();
- if($cell->getDataType()==PHPExcel_Cell_DataType::TYPE_NUMERIC){
- $cellstyleformat=$cell->getParent()->getStyle( $cell->getCoordinate() )->getNumberFormat();
- $formatcode=$cellstyleformat->getFormatCode();
- if (preg_match('/^(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy]/i', $formatcode)) {
- $value=gmdate("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($value));
- }else{
- $value=PHPExcel_Style_NumberFormat::toFormattedString($value,$formatcode);
- }
- }
复制代码
以上用到的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
|