Excel5 xls 文件导出方法

/**

* Excel5 xls 文件导出

*/

function downExcel($filename,$data,$key=array(),$width=array()){

    set_time_limit(1000);

    vendor("PHPExcel.PHPExcel");

    vendor("PHPExcel.PHPExcel.IOFactory");

    $chars = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z','AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL','AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY','AZ');

    $objPHPExcel = new \PHPExcel();

    $objPHPExcel->getActiveSheet()->setTitle("Excel1");

    foreach ($width as $k=>$v){

        $objPHPExcel->getActiveSheet()->getColumnDimension($k)->setWidth($v);

    }

    $i=1;

    foreach($data as $k=>$value){

        $num  =  count($value);

        for($a=0;$a<$num;$a++){

            $objPHPExcel->getActiveSheet()->getStyle($chars[$a].$i)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);

            if(is_array($value)){

                if(empty($key)){

                    $objPHPExcel->getActiveSheet()->setCellValueExplicit($chars[$a].$i,$value[$a],\PHPExcel_Cell_DataType::TYPE_STRING);

                }else{

                    $objPHPExcel->getActiveSheet()->setCellValueExplicit($chars[$a].$i,$value[$key[$a]],\PHPExcel_Cell_DataType::TYPE_STRING);

                }

            }else{

                $objPHPExcel->getActiveSheet()->setCellValueExplicit($chars[$a].$i,$value,\PHPExcel_Cell_DataType::TYPE_STRING);

            }

}

        $i++;

    }

    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

    if($filename=='')$filename = time();

    header('Content-Type: application/vnd.ms-excel');

    /*IE 中文乱码*/

    if ( preg_match( '/MSIE/i',  $_SERVER['HTTP_USER_AGENT'] ) ) {

        $filename = urlencode($filename);

    }

    $filename = iconv('UTF-8', 'GBK//IGNORE', $filename);

    header('Content-Disposition: attachment;filename="'.$filename.'.xls"');

    header('Cache-Control: max-age=0');

    $objWriter->save('php://output');

    exit(0);

}

你可能感兴趣的:(Excel5 xls 文件导出方法)