phpExcel导入支持A-ZZ

phpExcel导入支持A-ZZ


A-ZZ 共支持702个字段
可自己修改下面代码,支持更多列

$az = array();
foreach ($fileds as $key => $val) {
	$azsnum = 65+$key;
	if ($azsnum <= 90) {
		$azs = chr($azsnum);
	} else {
		$floor = floor(($azsnum - 65)/26) - 1;
		$azs = chr(65 + $floor).chr((($azsnum - 65) % 26) + 65);
	}
       $objPHPExcel->getActiveSheet()->getStyle($azs)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
       $az[$key] = $azs;
}

完整代码:


<?php
public function userprint()
{
    $fileds = array(
		array('field' => '数据库字段名','title' => '数据库字段备注')
	);
   $this->phpexcelprint(输出数据,$fileds,输出文件名);
}
private function phpexcelprint($list,$fileds,$title)
{
	vendor("PHPExcel");
	$objPHPExcel = new \PHPExcel();

    $objPHPExcel->getProperties()->setCreator("ctos")
        ->setLastModifiedBy("ctos")
        ->setTitle("Office 2007 XLSX Test Document")
        ->setSubject("Office 2007 XLSX Test Document")
        ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
        ->setKeywords("office 2007 openxml php")
        ->setCategory("Test result file");

    
	$az = array();
	foreach ($fileds as $key => $val) {
		$azsnum = 65+$key;
		if ($azsnum <= 90) {
			$azs = chr($azsnum);
		} else {
			$floor = floor(($azsnum - 65)/26) - 1;
			$azs = chr(65 + $floor).chr((($azsnum - 65) % 26) + 65);
		}
        $objPHPExcel->getActiveSheet()->getStyle($azs)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $az[$key] = $azs;
	}

    $newaz = array();
    foreach ($fileds as $key => $val) {
    	$objPHPExcel->setActiveSheetIndex(0)->setCellValue($az[$key].'1',$val['title']);
    	$newaz[$az[$key]] = $val['field'];
	}
    

    for($i=0;$i<count($list);$i++){
    	foreach ($newaz as $k => $v) {
    		$objPHPExcel->getActiveSheet(0)->setCellValue($k.($i+2),$list[$i][$v]);
    	}
    }
    $objPHPExcel->setActiveSheetIndex(0);
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="'.$title.'.xls"');
    header('Cache-Control: max-age=0');
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页