本站即日起正式部署SSL证书启用https安全连接, 联系站长:416326801#qq.com(#改成@)

Latest from the blog

PHP导出Excel文件

标签: PHP Excel

<?php  
//输出的文件类型为excel  
header("Content-type:application/vnd.ms-excel");  
//提示下载  
header("Content-Disposition:attachement;filename=jishu8_".date("Ymd").".xls");  
//报表数据  
$ReportArr = array(array(1,2,3,4,5),  
                    array('A','B','C','D','E'),  
                    array('up','down','left','right','center'),  
                    array('欢','迎','光','临','技','术','吧','!')  
                );  
$ReportContent = '';  
$num1 = count($ReportArr);  
for($i=0;$i<$num1;$i++){  
    $num2 = count($ReportArr[$i]);  
    for($j=0;$j<$num2;$j++){  
        //ecxel都是一格一格的,用\t将每一行的数据连接起来  
        $ReportContent .= '"'.$ReportArr[$i][$j].'"'."\t";  
    }  
    //最后连接\n 表示换行  
    $ReportContent .= "\n";  
}  
//用的utf-8 最后转换一个编码为gb  
$ReportContent = mb_convert_encoding($ReportContent,"gb2312","utf-8");  
//输出即提示下载  
echo $ReportContent;  
?>  

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;
}