0
点赞
收藏
分享

微信扫一扫

phpexcel 支持Php 5.2 php读取excel数据 演示

zibianqu 2023-05-19 阅读 37


phpexcel停止更新了 phpexcel 支持Php 5.2 

下载地址

https://github.com/PHPOffice/PHPExcel

最新用这个

https://github.com/PHPOffice/PhpSpreadsheet

<?php

error_reporting(E_ALL);
set_time_limit(0);

date_default_timezone_set('Europe/London');
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');

/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';


$inputFileType = 'Excel5';
//    $inputFileType = 'Excel2007';
//    $inputFileType = 'Excel2003XML';
//    $inputFileType = 'OOCalc';
//    $inputFileType = 'Gnumeric';
$inputFileName = './sampleData/example2.xls';
/**对excel里的日期进行格式转化*/
function GetData($val){
    $jd = GregorianToJD(1, 1, 1970);
    $gregorian = JDToGregorian($jd+intval($val)-25569);
    return $gregorian;/**显示格式为 “月/日/年” */
}
 
$filePath = './sampleData/example2.xls';
 
$PHPExcel = new PHPExcel();
 
/**默认用excel2007读取excel,若格式不对,则用之前的版本进行读取*/
$PHPReader = new PHPExcel_Reader_Excel2007();
if(!$PHPReader->canRead($filePath)){
    $PHPReader = new PHPExcel_Reader_Excel5();
    if(!$PHPReader->canRead($filePath)){
        echo 'no Excel';
        return ;
    }
}
 
$PHPExcel = $PHPReader->load($filePath);
/**读取excel文件中的第一个工作表*/
$currentSheet = $PHPExcel->getSheet(0);
/**取得最大的列号*/
$allColumn = $currentSheet->getHighestColumn();
/**取得一共有多少行*/
$allRow = $currentSheet->getHighestRow();
/**从第二行开始输出,因为excel表中第一行为列名*/
for($currentRow = 2;$currentRow <= $allRow;$currentRow++){
    
    echo  "$currentRow --------------------------\r\n";
/**从第A列开始输出*/
    for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++){
        $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,
        $currentRow)->getValue();/**ord()将字符转为十进制数*/
        //列的名称是 A B C D 这样定义的
    if($currentColumn == 'A') {

       //转换成日期。如果不是日期数据 不用这个
        echo gmdate("Y-m-d H:i:s", PHPExcel_Shared_Date::ExcelToPHP($val));

    }else{
    //echo $val; 
    /**如果输出汉字有乱码,则需将输出内容用iconv函数进行编码转换,如下将gb2312编码转为utf-8编码输出*/        
        echo iconv('utf-8','gb2312', $val)."\t";  
 
    }
}
}
?>

 

举报

相关推荐

0 条评论