PHPExcel style control

WBOY
Release: 2016-07-30 13:30:33
Original
1521 people have browsed it

Use PHPExcel to export files

PHPExcel to export mysql database data

The above article is for reference

The following is the code for PHPExcel style setting:

<span style="font-size:24px;"><?php
	$dir = dirname(__FILE__);
	require $dir."/db.php";
	require $dir."/PHPExcel.php";

	$db = new db($phpexcel);
	$objPHPExcel = new PHPExcel();

	for($i=0; $i<3; $i++){
		if($i>0){
			$objPHPExcel->createSheet();
		}
		$objPHPExcel->setActiveSheetIndex($i);
		$objSheet = $objPHPExcel->getActiveSheet();
		$objSheet->getColumnDimension('D')->setWidth(21); //设置列宽
		$objSheet->getColumnDimension('E')->setWidth(16);
		$objSheet->getColumnDimension('F')->setWidth(21);
		$objSheet->getRowDimension('1')->setRowHeight(80); //设置行高
		$objSheet->getRowDimension('2')->setRowHeight(29);
		$objSheet->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER)
							->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
													//设置水平垂直居中
		$objSheet->getDefaultStyle()->getFont()->setName("微软雅黑")->setSize(12); //设置默认字体大小
		$objSheet->getStyle("A1:F1")->getFont()->setSize(20)->setBold(true); //标题字体
		$objSheet->getStyle('A1:F1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
						->getStartColor()->setARGB('FFFF0000'); //设置标题背景颜色
		$objSheet->getStyle("A1:F1")->applyFromArray(getBorderStyle("#66FF99")); //设置标题边框

		$data = $db->getUserinfo();

		$j = 1;
		$objSheet->setCellValue("A".$j,"****\n****");
		$objSheet->getStyle('A1')->getAlignment()->setWrapText(true); //设置换行
		$objSheet->mergeCells("A".$j.":F".$j); //合并单元格

		$j++;
		$objSheet->setCellValue("A".$j,"编号")->setCellValue("B".$j,"登陆名")
				->setCellValue("C".$j,"昵称")->setCellValue("D".$j,"电子邮箱")
				->setCellValue("E".$j,"学校")->setCellValue("F".$j,"最后登陆时间")
				->setCellValue("G".$j,"随机数");

		$j++;
		foreach ($data as $key => $value) {
			# code...
			$objSheet->setCellValue("A".$j,$value['id'])->setCellValue("B".$j,$value['user_login'])
					->setCellValue("C".$j,$value['user_nicename'])->setCellValue("D".$j,$value['user_email'])
					->setCellValue("E".$j,$value['sch_name'])->setCellValue("F".$j,$value['last_login_time'])
					//显示数字的方法 1.指定为字符串 2.设置格式
					//->setCellValueExplicit("G".$j,rand(1000000000,9999999999),PHPExcel_Cell_DataType::TYPE_STRING);
					->setCellValue("G".$j,rand(100000000,999999999)); //下面以文本格式显示数字
			$objSheet->getStyle('G'.$j)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);

			$j++;
		}
	}

	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,"Excel5");
	// $objWriter->save($dir.'/export.xls'); //生成excel文件
	browser_export("Excel5","browser_excel03.xls"); //浏览器输出
	$objWriter->save("php://output");

	function browser_export($type, $filename){
		if($type == "Excel5"){
			header('Content-Type: application/vnd.ms-excel'); //excel2003
		}else{
			header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //excel2007
		}
		header('Content-Disposition: attachment;filename="'.$filename.'"');
		header('Cache-Control: max-age=0');
	}


	/*
	*获得不同颜色的边框
	*/
	function getBorderStyle($color){
		$styleArray = array(
			'borders' => array(
				'outline' => array(
					'style' => PHPExcel_Style_Border::BORDER_THICK,
					'color' => array('rgb' => $color),
				),
			),
		);
		return $styleArray;
	}

	

	
</span>
Copy after login
PHPExcel style function can refer to programming Document

For example, the number format we used above displays:


Ctrl + left mouse button to open


The code is all written, we Just use it, it's very convenient. There are all the setting functions in the document, and you can look it up when needed.

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above has introduced PHPExcel style control, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template