• php读取excel(支持03,07)


    需要用到PHPExcel这个类

    附上代码

     1 //phpExcel读取excel内容
     2 header("Content-Type:textml;charset=utf-8");
     3 //引用PHPExcel.php
     4 require_once '../js/PHPExcel.php';
     5 //文件路径
     6 $filePath = "03711.xlsx";
     7 //建立readerExcel对象
     8 $PHPReader = new PHPExcel_Reader_Excel2007();
     9 /**默认用excel2007读取excel,若格式不对,则用之前的版本进行读取*/
    10 if(!$PHPReader->canRead($filePath)){
    11     $PHPReader = new PHPExcel_Reader_Excel5();
    12     if(!$PHPReader->canRead($filePath)){
    13         exit('请使用03/07版本excel');
    14     }
    15 }
    16 
    17 //建立excel对象,此时你即可以读取文件excel文件内容
    18 $PHPExcel = $PHPReader->load($filePath);
    19 /**读取excel文件中的第一个工作表*/
    20 $currentSheet = $PHPExcel->getSheet(0);
    21 /**取得当前sheet名称*/
    22 $currentSheetName=$currentSheet->getTitle();
    23 /**取得sheet总数*/
    24 $allSheet=$PHPExcel->getSheetCount();
    25 /**取得最大的列号*/
    26 $allColumn = $currentSheet->getHighestColumn();
    27 /**转换为数字列数*/
    28 $allColumn=PHPExcel_Cell::columnIndexFromString($allColumn);
    29 /**取得一共有多少行*/
    30 $allRow = $currentSheet->getHighestRow();
    31 /*取得某一列的值  */
    32 $value->getCellByColumnAndRow(0, 1)->getValue();
    33 /* 这样就可以把excel的全部内容读取出来 */
    34 /* 时间转换 */
    35 $value=date("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($value) - 28800);

    完整版的

    $insert_flag = true; //是否写数据库标志
    $errorinfo = array(); //错误信息数组
    $success_num=0;
    $errorinfo1='不能为空';
    $errorinfo2='结束时间不能小于开始时间';
    $errorinfo3='所填年份在数据库中不存在';
    $errorinfo4='写数据失败';
    //文件路径
    //$filePath = "../03711.xlsx";
    //建立readerExcel对象
    $PHPReader = new PHPExcel_Reader_Excel2007();
    /**默认用excel2007读取excel,若格式不对,则用之前的版本进行读取*/
    if(!$PHPReader->canRead($filepath)){
        $PHPReader = new PHPExcel_Reader_Excel5();
        if(!$PHPReader->canRead($filepath)){
            exit('请使用03/07版本excel');
        }
    }
    
    //建立excel对象,此时你即可以读取文件excel文件内容
    $PHPExcel = $PHPReader->load($filepath);
    /**读取excel文件中的第一个工作表*/
    $currentSheet = $PHPExcel->getSheet(0);
    /**取得当前sheet名称*/
    $currentSheetName=$currentSheet->getTitle();
    /**取得sheet总数*/
    $allSheet=$PHPExcel->getSheetCount();
    /**取得最大的列号*/
    $allColumn = $currentSheet->getHighestColumn();
    /**转换为数字列数*/
    $allColumn=PHPExcel_Cell::columnIndexFromString($allColumn);
    /**取得一共有多少行*/
    $allRow = $currentSheet->getHighestRow();
    
    //echo "列:".$allColumn."行:".$allRow."<br/>";
    
    
    /* 从第二行开始读取 */
    for ($currentRow=2;$currentRow<=$allRow;$currentRow++){
        /*取得当前行每一列的值 ,列从0开始 */
        //$year=$currentSheet->getCellByColumnAndRow(1, $currentRow)->getValue();
        $year=$_GET['yearId'];
        $startTime=$currentSheet->getCellByColumnAndRow(1, $currentRow)->getValue();
        $endTime=$currentSheet->getCellByColumnAndRow(2, $currentRow)->getValue();
        $remark=$currentSheet->getCellByColumnAndRow(3, $currentRow)->getValue();
        /*如果有一列的值为空,就跳出循环  */
        if ($startTime==''||$endTime==''||$year==''){
            $errorinfo[]=$errorinfo1.'|'.$currentRow;
            continue;
        }
        if ($endTime<$startTime){
            echo $startTime.'|'.$endTime;
            $errorinfo[]=$errorinfo2.'|'.$currentRow;
            continue;
        }
        $startTime=date("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($startTime) - 28800);
        $endTime=date("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($endTime) - 28800);
        $shorStartTime=str_replace('-', '.', substr($startTime, 5));
        $shorEndTime=str_replace('-', '.', substr($endTime, 5));
    //     echo $year."|".$startTime."|".$endTime."|".$remark."<br/>";
        if ($insert_flag){
            $rs = $db -> select(0, 1, 't_year', 'id,name', ' where id ="'. $year .'"' , 'id limit 1');
            if (count($rs['id'])<1){
                $errorinfo[]=$errorinfo3.'|'.$currentRow;
                continue;
            }
            if ($remark==''){
                $sql='"';
            }else{
                $sql='('.$remark.')"';
            }
            $flag=$db -> insert(0, 1, 't_period', 'year_id='.$rs['id'].',begtime="'.$startTime.'",endtime="'.$endTime.
            '",remark="'.$remark.'",timename="'.$shorStartTime.'-'.$shorEndTime.$sql);
            if ($flag!=1){
                $errorinfo[]=$errorinfo4.'|'.$currentRow;
            }else{
                $success_num = $success_num + 1;
            }
        }
        
    }
    
    unlink($filepath);
    if (count($errorinfo)>0){
        echo '<span style="color:#F00" >一共导入了' . $success_num . '条记录<br>';
        echo "以下数据没有导入:<br>";
        for ($i = 0; $i < count($errorinfo); $i++) {
            $temp_info = explode("|", $errorinfo[$i]);
            echo "" . $temp_info[1] . "行错误信息:" . $temp_info[0] . "<br>";
        }
    } else {
        echo "数据导入成功<br>";
        echo "一共导入了" . $success_num . "条记录<br></span>";
    }

     下载地址:http://pan.baidu.com/s/1i35ppBJ

    完整版是api,另外一个是程序中用的。

  • 相关阅读:
    Python 2 与 python 3的区别
    语法基础题
    Python运算符_ 2018-07-26
    Python 各种语句:2018-07-27
    解决在Python中使用Win32api报错的问题,No module named win32api
    在CenOS7.5里安装Redis
    下载Redis
    重置CentOS6.5的登录口令
    如何在CentOS里切换操作系统所用的语言,中英文切换
    在Ubuntu里安装Mysql5.7.23
  • 原文地址:https://www.cnblogs.com/blueskycc/p/4348428.html
Copyright © 2020-2023  润新知