联系我们:416326801#qq.com(#改成@)

Latest from the blog

PHP读取CSV数据

标签: PHP Excel

<?php
$file = fopen($_FILES['file']['tmp_name'], 'r');
while ( $data = fgetcsv($file) ) { //每次读取CSV里面的一行内容
/*
    foreach ( $data as &$v ) {
        $v = iconv('gbk', 'utf-8', $v);
    }
*/
    $list[] = $data;
}
    fclose($file);
print_r($list);
?>

PHP读取Excel函数Spreadsheet_Excel_Reader

标签: PHP Excel

<?php
require (__DIR__.'/Spreadsheet_Excel_Reader.class.php');
class ExcelReader {
        public static function readXLS($file){
                $data = new Spreadsheet_Excel_Reader();
                $data->setOutputEncoding('UTF-8'); //设置输出的编码为utf8
                $ret = $data->read($file); //要读取的excel文件地址
                if($ret == -1){
                        $array = array('ret'=>$ret);
                }else{
                        for ($i =1 ; $i <= $data->sheets[0]['numRows']; $i++) {
                                for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
                                        $array[$i-1][$j-1] = $data->sheets[0]['cells'][$i][$j];
                                }
                        }
                }
                if ( $array && is_array($array) ) {
                        return $array;
                }
                return array();
        }
}
?>
Spreadsheet_Excel_Reader类下载:Spreadsheet_Excel_Reader.class.zip

PHP导入Excel时间格式转换

标签: PHP Excel

public static function excelTime($date, $time=false){
        if(is_numeric($date)) {
                $jd = GregorianToJD(1, 1, 1970);
                $gregorian = JDToGregorian($jd+intval($date)-25569);
                $date = explode('/',$gregorian);
                $date_str = str_pad($date[2],4,'0', STR_PAD_LEFT)
                ."-".str_pad($date[0],2,'0', STR_PAD_LEFT)
                ."-".str_pad($date[1],2,'0', STR_PAD_LEFT)
                .($time?" 00:00:00":'');
                return $date_str;
        }
        return $date;
}