php导出excel

            php导出excel 代码如下(先下载PHPExcel包)

        header("Content-Type: text/html;charset=gbk");
        require_once 'Classes/PHPExcel.php';
        require_once 'Classes/PHPExcel/IOFactory.php';
        require_once 'Classes/PHPExcel/Reader/Excel5.php';
	
	$objReader = PHPExcel_IOFactory::createReader('Excel2007');//use excel2007 for 2007 format
	$objPHPExcel1 = new PHPExcel();
	$objPHPExcel1->getProperties()->setTitle("export")->setDescription("none");
	$objPHPExcel1 ->setActiveSheetIndex(0)
	//Excel的第A列,uid是你查出数组的键值,下面以此类推
	->setCellValue('A1', '序号')
	->setCellValue('B1', '姓名')
	->setCellValue('C1', '手机号码')
	->setCellValue('D1', '租房地址');
	$objPHPExcel1->getActiveSheet()->getColumnDimension('A')->setWidth(10);
	$objPHPExcel1->getActiveSheet()->getColumnDimension('B')->setWidth(10);
	$objPHPExcel1->getActiveSheet()->getColumnDimension('C')->setWidth(15);
	$objPHPExcel1->getActiveSheet()->getColumnDimension('D')->setWidth(40);

	$userQuery = "select * from test by modified crtime";
	$row=mysql_query($userQuery);
	$k= 1;
	while($result = mysql_fetch_array($row)){
		$num=$k+1;
		$k++;
		$objPHPExcel1 ->setActiveSheetIndex(0)
		//Excel的第A列,uid是你查出数组的键值,下面以此类推
		->setCellValue('A'.$num, $num-1)
		->setCellValue('B'.$num, ''.$result['userName'])
		->setCellValue('C'.$num, ' '.$result['userTel'] )
		->setCellValue('D'.$num, ''.$result['address']);
	}
	$objPHPExcel1 ->getActiveSheet()->setTitle(date("YmdHis",time()));
	$objPHPExcel1 ->setActiveSheetIndex(0);
	ob_end_clean();//清除缓冲区,避免乱码
	header('Content-Type: application/vnd.ms-excel');
	header('Content-Disposition: attachment;filename="'."订单_".date("YmdHis",time()).'.xls"');
	header('Cache-Control: max-age=0');
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel1, 'Excel5');
	$objWriter->save('php://output');
	exit;

}

php导出excel代码 拿去不谢。

喜欢(0)

评论 抢沙发

表情